]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
d4180770f711cdbbc62cf0e581e47936be06f0c9
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/socket.h>
31 #include <ac/string.h>
32 #include <ac/time.h>
33
34 #include "slap.h"
35 #include "lutil.h"
36
37
38 int
39 do_modify(
40     Operation   *op,
41     SlapReply   *rs )
42 {
43         struct berval dn = BER_BVNULL;
44         char            textbuf[ SLAP_TEXT_BUFLEN ];
45         size_t          textlen = sizeof( textbuf );
46
47         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
48
49         /*
50          * Parse the modify request.  It looks like this:
51          *
52          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
53          *              name    DistinguishedName,
54          *              mods    SEQUENCE OF SEQUENCE {
55          *                      operation       ENUMERATED {
56          *                              add     (0),
57          *                              delete  (1),
58          *                              replace (2)
59          *                      },
60          *                      modification    SEQUENCE {
61          *                              type    AttributeType,
62          *                              values  SET OF AttributeValue
63          *                      }
64          *              }
65          *      }
66          */
67
68         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
69                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
70
71                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
72                 return SLAPD_DISCONNECT;
73         }
74
75         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
76
77         rs->sr_err = slap_parse_modlist( op, rs, op->o_ber, &op->oq_modify );
78         if ( rs->sr_err != LDAP_SUCCESS ) {
79                 Debug( LDAP_DEBUG_ANY, "do_modify: slap_parse_modlist failed err=%d msg=%s\n",
80                         rs->sr_err, rs->sr_text, 0 );
81                 goto cleanup;
82         }
83
84         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
85                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
86                 goto cleanup;
87         }
88
89         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
90                 op->o_tmpmemctx );
91         if( rs->sr_err != LDAP_SUCCESS ) {
92                 Debug( LDAP_DEBUG_ANY,
93                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
94                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
95                 goto cleanup;
96         }
97
98         op->orm_no_opattrs = 0;
99
100         rs->sr_err = slap_mods_check( op, op->orm_modlist,
101                 &rs->sr_text, textbuf, textlen, NULL );
102
103         if ( rs->sr_err != LDAP_SUCCESS ) {
104                 send_ldap_result( op, rs );
105                 goto cleanup;
106         }
107
108         op->o_bd = frontendDB;
109         rs->sr_err = frontendDB->be_modify( op, rs );
110
111 #ifdef LDAP_X_TXN
112         if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
113                 /* skip cleanup */
114                 return rs->sr_err;
115         }
116 #endif
117
118 cleanup:
119         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
120         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
121         if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
122
123         return rs->sr_err;
124 }
125
126 int
127 fe_op_modify( Operation *op, SlapReply *rs )
128 {
129 #ifdef LDAP_DEBUG
130         Modifications   *tmp;
131 #endif
132         BackendDB       *op_be, *bd = op->o_bd;
133         char            textbuf[ SLAP_TEXT_BUFLEN ];
134         size_t          textlen = sizeof( textbuf );
135         
136         if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
137                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
138
139                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
140                         "modify upon the root DSE not supported" );
141                 goto cleanup;
142
143         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
144                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
145
146                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
147                         "modification of subschema subentry not supported" );
148                 goto cleanup;
149         }
150
151 #ifdef LDAP_DEBUG
152         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
153
154         for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
155                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
156                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
157                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
158                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
159                                         "replace")), tmp->sml_type.bv_val, 0 );
160
161                 if ( tmp->sml_values == NULL ) {
162                         Debug( LDAP_DEBUG_ARGS, "%s\n",
163                            "\t\tno values", NULL, NULL );
164                 } else if ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
165                         Debug( LDAP_DEBUG_ARGS, "%s\n",
166                            "\t\tzero values", NULL, NULL );
167                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
168                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
169                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
170                 } else {
171                         Debug( LDAP_DEBUG_ARGS, "%s\n",
172                            "\t\tmultiple values", NULL, NULL );
173                 }
174         }
175
176         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
177                 char abuf[BUFSIZ/2], *ptr = abuf;
178                 int len = 0;
179
180                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
181                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
182
183                 for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
184                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
185                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
186                                     op->o_log_prefix, abuf, 0, 0, 0 );
187
188                                 len = 0;
189                                 ptr = abuf;
190
191                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
192                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
193                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
194                                         continue;
195                                 }
196                         }
197                         if (len) {
198                                 *ptr++ = ' ';
199                                 len++;
200                         }
201                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
202                         len += tmp->sml_type.bv_len;
203                 }
204                 if (len) {
205                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
206                                 op->o_log_prefix, abuf, 0, 0, 0 );
207                 }
208         }
209 #endif  /* LDAP_DEBUG */
210
211         /*
212          * We could be serving multiple database backends.  Select the
213          * appropriate one, or send a referral to our "referral server"
214          * if we don't hold it.
215          */
216         op->o_bd = select_backend( &op->o_req_ndn, 1 );
217         if ( op->o_bd == NULL ) {
218                 op->o_bd = bd;
219                 rs->sr_ref = referral_rewrite( default_referral,
220                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
221                 if ( !rs->sr_ref ) {
222                         rs->sr_ref = default_referral;
223                 }
224
225                 if ( rs->sr_ref != NULL ) {
226                         rs->sr_err = LDAP_REFERRAL;
227                         send_ldap_result( op, rs );
228
229                         if ( rs->sr_ref != default_referral ) {
230                                 ber_bvarray_free( rs->sr_ref );
231                         }
232
233                 } else {
234                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
235                                 "no global superior knowledge" );
236                 }
237                 goto cleanup;
238         }
239
240         /* If we've got a glued backend, check the real backend */
241         op_be = op->o_bd;
242         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
243                 op->o_bd = select_backend( &op->o_req_ndn, 0 );
244         }
245
246         /* check restrictions */
247         if ( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
248                 send_ldap_result( op, rs );
249                 goto cleanup;
250         }
251
252         /* check for referrals */
253         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
254                 goto cleanup;
255         }
256
257         rs->sr_err = slap_mods_obsolete_check( op, op->orm_modlist,
258                 &rs->sr_text, textbuf, textlen );
259         if ( rs->sr_err != LDAP_SUCCESS ) {
260                 send_ldap_result( op, rs );
261                 goto cleanup;
262         }
263
264         /* check for modify/increment support */
265         if ( op->orm_increment && !SLAP_INCREMENT( op->o_bd ) ) {
266                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
267                         "modify/increment not supported in context" );
268         }
269
270         /*
271          * do the modify if 1 && (2 || 3)
272          * 1) there is a modify function implemented in this backend;
273          * 2) this backend is master for what it holds;
274          * 3) it's a replica and the dn supplied is the update_ndn.
275          */
276         if ( op->o_bd->be_modify ) {
277                 /* do the update here */
278                 int repl_user = be_isupdate( op );
279
280                 /*
281                  * Multimaster slapd does not have to check for replicator dn
282                  * because it accepts each modify request
283                  */
284                 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
285                         int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
286
287                         op->o_bd = op_be;
288
289                         if ( !update ) {
290                                 rs->sr_err = slap_mods_no_user_mod_check( op, op->orm_modlist,
291                                         &rs->sr_text, textbuf, textlen );
292                                 if ( rs->sr_err != LDAP_SUCCESS ) {
293                                         send_ldap_result( op, rs );
294                                         goto cleanup;
295                                 }
296                         }
297                         op->o_bd->be_modify( op, rs );
298
299                 } else { /* send a referral */
300                         BerVarray defref = op->o_bd->be_update_refs
301                                 ? op->o_bd->be_update_refs : default_referral;
302                         if ( defref != NULL ) {
303                                 rs->sr_ref = referral_rewrite( defref,
304                                         NULL, &op->o_req_dn,
305                                         LDAP_SCOPE_DEFAULT );
306                                 if ( rs->sr_ref == NULL ) {
307                                         /* FIXME: must duplicate, because
308                                          * overlays may muck with it */
309                                         rs->sr_ref = defref;
310                                 }
311                                 rs->sr_err = LDAP_REFERRAL;
312                                 send_ldap_result( op, rs );
313                                 if ( rs->sr_ref != defref ) {
314                                         ber_bvarray_free( rs->sr_ref );
315                                 }
316
317                         } else {
318                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
319                                         "shadow context; no update referral" );
320                         }
321                 }
322
323         } else {
324                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
325                     "operation not supported within namingContext" );
326         }
327
328 cleanup:;
329         op->o_bd = bd;
330         return rs->sr_err;
331 }
332
333 /*
334  * Obsolete constraint checking.
335  */
336 int
337 slap_mods_obsolete_check(
338         Operation *op,
339         Modifications *ml,
340         const char **text,
341         char *textbuf,
342         size_t textlen )
343 {
344         if( get_relax( op ) ) return LDAP_SUCCESS;
345
346         for ( ; ml != NULL; ml = ml->sml_next ) {
347                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
348                         (( ml->sml_op != LDAP_MOD_REPLACE &&
349                                 ml->sml_op != LDAP_MOD_DELETE ) ||
350                                         ml->sml_values != NULL ))
351                 {
352                         /*
353                          * attribute is obsolete,
354                          * only allow replace/delete with no values
355                          */
356                         snprintf( textbuf, textlen,
357                                 "%s: attribute is obsolete",
358                                 ml->sml_type.bv_val );
359                         *text = textbuf;
360                         return LDAP_CONSTRAINT_VIOLATION;
361                 }
362         }
363
364         return LDAP_SUCCESS;
365 }
366
367 /*
368  * No-user-modification constraint checking.
369  */
370 int
371 slap_mods_no_user_mod_check(
372         Operation *op,
373         Modifications *ml,
374         const char **text,
375         char *textbuf,
376         size_t textlen )
377 {
378         for ( ; ml != NULL; ml = ml->sml_next ) {
379                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
380                         continue;
381                 }
382
383                 if ( get_relax( op ) ) {
384                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
385                                 ml->sml_flags |= SLAP_MOD_MANAGING;
386                                 continue;
387                         }
388
389                         /* attribute not manageable */
390                         snprintf( textbuf, textlen,
391                                 "%s: no-user-modification attribute not manageable",
392                                 ml->sml_type.bv_val );
393
394                 } else {
395                         /* user modification disallowed */
396                         snprintf( textbuf, textlen,
397                                 "%s: no user modification allowed",
398                                 ml->sml_type.bv_val );
399                 }
400
401                 *text = textbuf;
402                 return LDAP_CONSTRAINT_VIOLATION;
403         }
404
405         return LDAP_SUCCESS;
406 }
407
408 int
409 slap_mods_no_repl_user_mod_check(
410         Operation *op,
411         Modifications *ml,
412         const char **text,
413         char *textbuf,
414         size_t textlen )
415 {
416         Modifications *mods;
417         Modifications *modp;
418
419         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
420                 assert( mods->sml_op == LDAP_MOD_ADD );
421
422                 /* check doesn't already appear */
423                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
424                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
425                                 snprintf( textbuf, textlen,
426                                         "attribute '%s' provided more than once",
427                                         mods->sml_desc->ad_cname.bv_val );
428                                 *text = textbuf;
429                                 return LDAP_TYPE_OR_VALUE_EXISTS;
430                         }
431                 }
432         }
433
434         return LDAP_SUCCESS;
435 }
436
437 /*
438  * Do basic attribute type checking and syntax validation.
439  */
440 int slap_mods_check(
441         Operation *op,
442         Modifications *ml,
443         const char **text,
444         char *textbuf,
445         size_t textlen,
446         void *ctx )
447 {
448         int rc;
449
450         for( ; ml != NULL; ml = ml->sml_next ) {
451                 AttributeDescription *ad = NULL;
452
453                 /* convert to attribute description */
454                 if ( ml->sml_desc == NULL ) {
455                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
456                         if( rc != LDAP_SUCCESS ) {
457                                 if ( get_no_schema_check( op )) {
458                                         rc = slap_bv2undef_ad( &ml->sml_type, &ml->sml_desc,
459                                                 text, 0 );
460                                 }
461                         }
462                         if( rc != LDAP_SUCCESS ) {
463                                 snprintf( textbuf, textlen, "%s: %s",
464                                         ml->sml_type.bv_val, *text );
465                                 *text = textbuf;
466                                 return rc;
467                         }
468                 }
469
470                 ad = ml->sml_desc;
471
472                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
473                         && !slap_ad_is_binary( ad ))
474                 {
475                         /* attribute requires binary transfer */
476                         snprintf( textbuf, textlen,
477                                 "%s: requires ;binary transfer",
478                                 ml->sml_type.bv_val );
479                         *text = textbuf;
480                         return LDAP_UNDEFINED_TYPE;
481                 }
482
483                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
484                         && slap_ad_is_binary( ad ))
485                 {
486                         /* attribute does not require binary transfer */
487                         snprintf( textbuf, textlen,
488                                 "%s: disallows ;binary transfer",
489                                 ml->sml_type.bv_val );
490                         *text = textbuf;
491                         return LDAP_UNDEFINED_TYPE;
492                 }
493
494                 if( slap_ad_is_tag_range( ad )) {
495                         /* attribute requires binary transfer */
496                         snprintf( textbuf, textlen,
497                                 "%s: inappropriate use of tag range option",
498                                 ml->sml_type.bv_val );
499                         *text = textbuf;
500                         return LDAP_UNDEFINED_TYPE;
501                 }
502
503 #if 0
504                 if ( is_at_obsolete( ad->ad_type ) &&
505                         (( ml->sml_op != LDAP_MOD_REPLACE &&
506                                 ml->sml_op != LDAP_MOD_DELETE ) ||
507                                         ml->sml_values != NULL ))
508                 {
509                         /*
510                          * attribute is obsolete,
511                          * only allow replace/delete with no values
512                          */
513                         snprintf( textbuf, textlen,
514                                 "%s: attribute is obsolete",
515                                 ml->sml_type.bv_val );
516                         *text = textbuf;
517                         return LDAP_CONSTRAINT_VIOLATION;
518                 }
519 #endif
520
521                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
522 #ifdef SLAPD_REAL_SYNTAX
523                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
524 #endif
525                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
526                 {
527                         /*
528                          * attribute values must be INTEGER or REAL
529                          */
530                         snprintf( textbuf, textlen,
531                                 "%s: attribute syntax inappropriate for increment",
532                                 ml->sml_type.bv_val );
533                         *text = textbuf;
534                         return LDAP_CONSTRAINT_VIOLATION;
535                 }
536
537                 /*
538                  * check values
539                  */
540                 if( ml->sml_values != NULL ) {
541                         ber_len_t nvals;
542                         slap_syntax_validate_func *validate =
543                                 ad->ad_type->sat_syntax->ssyn_validate;
544                         slap_syntax_transform_func *pretty =
545                                 ad->ad_type->sat_syntax->ssyn_pretty;
546  
547                         if( !pretty && !validate ) {
548                                 *text = "no validator for syntax";
549                                 snprintf( textbuf, textlen,
550                                         "%s: no validator for syntax %s",
551                                         ml->sml_type.bv_val,
552                                         ad->ad_type->sat_syntax->ssyn_oid );
553                                 *text = textbuf;
554                                 return LDAP_INVALID_SYNTAX;
555                         }
556
557                         /*
558                          * check that each value is valid per syntax
559                          *      and pretty if appropriate
560                          */
561                         for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
562                                 struct berval pval;
563
564                                 if ( pretty ) {
565                                         rc = ordered_value_pretty( ad,
566                                                 &ml->sml_values[nvals], &pval, ctx );
567                                 } else {
568                                         rc = ordered_value_validate( ad,
569                                                 &ml->sml_values[nvals], ml->sml_op );
570                                 }
571
572                                 if( rc != 0 ) {
573                                         snprintf( textbuf, textlen,
574                                                 "%s: value #%ld invalid per syntax",
575                                                 ml->sml_type.bv_val, (long) nvals );
576                                         *text = textbuf;
577                                         return LDAP_INVALID_SYNTAX;
578                                 }
579
580                                 if( pretty ) {
581                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
582                                         ml->sml_values[nvals] = pval;
583                                 }
584                         }
585
586                         /*
587                          * a rough single value check... an additional check is needed
588                          * to catch add of single value to existing single valued attribute
589                          */
590                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
591                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
592                         {
593                                 snprintf( textbuf, textlen,
594                                         "%s: multiple values provided",
595                                         ml->sml_type.bv_val );
596                                 *text = textbuf;
597                                 return LDAP_CONSTRAINT_VIOLATION;
598                         }
599
600                         /* if the type has a normalizer, generate the
601                          * normalized values. otherwise leave them NULL.
602                          *
603                          * this is different from the rule for attributes
604                          * in an entry - in an attribute list, the normalized
605                          * value is set equal to the non-normalized value
606                          * when there is no normalizer.
607                          */
608                         if( nvals && ad->ad_type->sat_equality &&
609                                 ad->ad_type->sat_equality->smr_normalize )
610                         {
611                                 ml->sml_nvalues = ber_memalloc_x(
612                                         (nvals+1)*sizeof(struct berval), ctx );
613
614                                 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
615                                         rc = ordered_value_normalize(
616                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
617                                                 ad,
618                                                 ad->ad_type->sat_equality,
619                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
620                                         if ( rc ) {
621                                                 Debug( LDAP_DEBUG_ANY,
622                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
623                                                         rc, 0, 0 );
624                                                 snprintf( textbuf, textlen,
625                                                         "%s: value #%ld normalization failed",
626                                                         ml->sml_type.bv_val, (long) nvals );
627                                                 *text = textbuf;
628                                                 return rc;
629                                         }
630                                 }
631
632                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
633                         }
634
635                         /* check for duplicates, but ignore Deletes.
636                          */
637                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
638 #define SLAP_MODS_CHECK_QUICKSORT
639 #ifndef SLAP_MODS_CHECK_QUICKSORT
640                                 int             i, j, rc, match;
641                                 MatchingRule *mr = ad->ad_type->sat_equality;
642
643                                 for ( i = 1; i < nvals ; i++ ) {
644                                         /* test asserted values against themselves */
645                                         for( j = 0; j < i; j++ ) {
646                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
647                                                         SLAP_MR_EQUALITY
648                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
649                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
650                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
651                                                         ml->sml_nvalues
652                                                                 ? &ml->sml_nvalues[i]
653                                                                 : &ml->sml_values[i],
654                                                         ml->sml_nvalues
655                                                                 ? &ml->sml_nvalues[j]
656                                                                 : &ml->sml_values[j],
657                                                         text );
658                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
659                                                         /* value exists already */
660                                                         snprintf( textbuf, textlen,
661                                                                 "%s: value #%d provided more than once",
662                                                                 ml->sml_desc->ad_cname.bv_val, j );
663                                                         *text = textbuf;
664                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
665
666                                                 } else if ( rc != LDAP_SUCCESS ) {
667                                                         return rc;
668                                                 }
669                                         }
670                                 }
671 #else   /* SLAP_MODS_CHECK_QUICKSORT */
672
673 /* Quicksort + Insertion sort for small arrays */
674
675 #define SMALL   8
676 #define SWAP(a,b,tmp)   tmp=(a);(a)=(b);(b)=tmp
677 #define COMP(a,b)       match=0; rc = ordered_value_match( &match, \
678                                                 ml->sml_desc, mr, SLAP_MR_EQUALITY \
679                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX \
680                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH \
681                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH, \
682                                                                 &(a), &(b), text );
683
684                                         MatchingRule *mr = ad->ad_type->sat_equality;
685                                         int istack[sizeof(int)*16];
686                                         int i, j, k, l, ir, jstack, match, *ix, itmp;
687                                         struct berval a, *cv;
688
689 /* If PRESERVE_ORDER is defined only the index array is sorted; the
690  * actual values are left in their incoming order. Otherwise, the
691  * only reason to keep the index array is to identify the offending
692  * value when duplicates are found.
693  */
694 #define PRESERVE_ORDER
695 #ifndef PRESERVE_ORDER
696                                         struct berval va, *v, *nv, bvtmp;
697
698 #define IX(x)   x
699 #define EXCH(x,y)       SWAP(ix[x],ix[y],itmp); SWAP(cv[x],cv[y],bvtmp); \
700         if (nv) {SWAP(v[x],v[y],bvtmp);}
701 #define SETA(x) itmp = ix[x]; a = cv[x]; if (nv) va=v[x]
702 #define GETA(x) ix[x] = itmp; cv[x] = a; if (nv) v[x]=va
703 #define SET(x,y)        ix[x] = ix[y]; cv[x] = cv[y]; if (nv) v[x]=v[y]
704
705                                         v = ml->sml_values;
706                                         nv = ml->sml_nvalues;
707
708 #else   /* PRESERVE_ORDER */
709
710 #define IX(x)   ix[x]
711 #define EXCH(x,y)       SWAP(ix[x],ix[y],itmp)
712 #define SETA(x) itmp = ix[x]; a = cv[itmp]
713 #define GETA(x) ix[x] = itmp;
714 #define SET(x,y)        ix[x] = ix[y]
715
716 #endif  /* PRESERVE_ORDER */
717
718                                         cv = ml->sml_nvalues ? ml->sml_nvalues : ml->sml_values;
719                                         if ( ad == slap_schema.si_ad_objectClass )
720                                                 mr = NULL;      /* shortcut matching */
721
722                                         /* record indices to preserve input ordering */
723                                         ix = slap_sl_malloc( nvals * sizeof(int), ctx );
724                                         for (i=0; i<nvals; i++) ix[i] = i;
725
726                                         ir = nvals-1;
727                                         l = 0;
728                                         jstack = 0;
729
730                                         for(;;) {
731                                                 if (ir - l < SMALL) {   /* Insertion sort */
732                                                         match=1;
733                                                         for (j=l+1;j<=ir;j++) {
734                                                                 SETA(j);
735                                                                 for (i=j-1;i>=0;i--) {
736                                                                         COMP(cv[IX(i)], a);
737                                                                         if ( match <= 0 )
738                                                                                 break;
739                                                                         SET(i+1,i);
740                                                                 }
741                                                                 GETA(i+1);
742                                                                 if ( match == 0 ) goto done;
743                                                         }
744                                                         if ( jstack == 0 ) break;
745                                                         if ( match == 0 ) break;
746                                                         ir = istack[jstack--];
747                                                         l = istack[jstack--];
748                                                 } else {
749                                                         k = (l + ir) >> 1;      /* Choose median of left, center, right */
750                                                         EXCH(k, l+1);
751                                                         COMP( cv[IX(l)], cv[IX(ir)] );
752                                                         if ( match > 0 ) {
753                                                                 EXCH(l, ir);
754                                                         } else if ( match == 0 ) {
755                                                                 i = ir;
756                                                                 break;
757                                                         }
758                                                         COMP( cv[IX(l+1)], cv[IX(ir)] );
759                                                         if ( match > 0 ) {
760                                                                 EXCH(l+1, ir);
761                                                         } else if ( match == 0 ) {
762                                                                 i = ir;
763                                                                 break;
764                                                         }
765                                                         COMP( cv[IX(l)], cv[IX(l+1)] );
766                                                         if ( match > 0 ) {
767                                                                 EXCH(l, l+1);
768                                                         } else if ( match == 0 ) {
769                                                                 i = l;
770                                                                 break;
771                                                         }
772                                                         i = l+1;
773                                                         j = ir;
774                                                         a = cv[IX(i)];
775                                                         for(;;) {
776                                                                 do {
777                                                                         i++;
778                                                                         COMP( cv[IX(i)], a );
779                                                                 } while( match < 0 );
780                                                                 while( match > 0 ) {
781                                                                         j--;
782                                                                         COMP( cv[IX(j)], a );
783                                                                 }
784                                                                 if (j < i) {
785                                                                         match = 1;
786                                                                         break;
787                                                                 }
788                                                                 if ( match == 0 ) {
789                                                                         i = l+1;
790                                                                         break;
791                                                                 }
792                                                                 EXCH(i,j);
793                                                         }
794                                                         if ( match == 0 )
795                                                                 break;
796                                                         EXCH(l+1,j);
797                                                         jstack += 2;
798                                                         if (ir-i+1 >= j) {
799                                                                 istack[jstack] = ir;
800                                                                 istack[jstack-1] = i;
801                                                                 ir = j;
802                                                         } else {
803                                                                 istack[jstack] = j;
804                                                                 istack[jstack-1] = l;
805                                                                 l = i;
806                                                 }
807                                         }
808                                 }
809 done:
810                                 if ( i >= 0 )
811                                         j = ix[i];
812
813                                 slap_sl_free( ix, ctx );
814
815                                 if ( rc != LDAP_SUCCESS ) {
816                                         return rc;
817                                 } else if ( match == 0 ) {
818                                         /* value exists already */
819                                         assert( i >= 0 );
820                                         assert( i < nvals );
821                                         snprintf( textbuf, textlen,
822                                                 "%s: value #%d provided more than once",
823                                                 ml->sml_desc->ad_cname.bv_val, j );
824                                         *text = textbuf;
825                                         return LDAP_TYPE_OR_VALUE_EXISTS;
826                                 }
827 #endif  /* SLAP_MODS_CHECK_QUICKSORT */
828                         }
829                 }
830         }
831
832         return LDAP_SUCCESS;
833 }
834
835 /* Enter with bv->bv_len = sizeof buffer, returns with
836  * actual length of string
837  */
838 void slap_timestamp( time_t *tm, struct berval *bv )
839 {
840         struct tm *ltm;
841 #ifdef HAVE_GMTIME_R
842         struct tm ltm_buf;
843
844         ltm = gmtime_r( tm, &ltm_buf );
845 #else
846         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
847         ltm = gmtime( tm );
848 #endif
849
850         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
851
852 #ifndef HAVE_GMTIME_R
853         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
854 #endif
855 }
856
857 /* Called for all modify and modrdn ops. If the current op was replicated
858  * from elsewhere, all of the attrs should already be present.
859  */
860 void slap_mods_opattrs(
861         Operation *op,
862         Modifications **modsp,
863         int manage_ctxcsn )
864 {
865         struct berval name, timestamp, csn = BER_BVNULL;
866         struct berval nname;
867         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
868         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
869         Modifications *mod, **modtail, *modlast;
870         int gotcsn = 0, gotmname = 0, gotmtime = 0;
871
872         if ( SLAP_LASTMOD( op->o_bd ) && !op->orm_no_opattrs ) {
873                 char *ptr;
874                 timestamp.bv_val = timebuf;
875                 for ( modtail = modsp; *modtail; modtail = &(*modtail)->sml_next ) {
876                         if ( (*modtail)->sml_op != LDAP_MOD_ADD &&
877                                 (*modtail)->sml_op != LDAP_MOD_REPLACE )
878                         {
879                                 continue;
880                         }
881
882                         if ( (*modtail)->sml_desc == slap_schema.si_ad_entryCSN )
883                         {
884                                 csn = (*modtail)->sml_values[0];
885                                 gotcsn = 1;
886
887                         } else if ( (*modtail)->sml_desc == slap_schema.si_ad_modifiersName )
888                         {
889                                 gotmname = 1;
890
891                         } else if ( (*modtail)->sml_desc == slap_schema.si_ad_modifyTimestamp )
892                         {
893                                 gotmtime = 1;
894                         }
895                 }
896
897                 if ( BER_BVISEMPTY( &op->o_csn )) {
898                         if ( !gotcsn ) {
899                                 csn.bv_val = csnbuf;
900                                 csn.bv_len = sizeof( csnbuf );
901                                 slap_get_csn( op, &csn, manage_ctxcsn );
902
903                         } else {
904                                 if ( manage_ctxcsn ) {
905                                         slap_queue_csn( op, &csn );
906                                 }
907                         }
908
909                 } else {
910                         csn = op->o_csn;
911                 }
912
913                 ptr = ber_bvchr( &csn, '#' );
914                 if ( ptr ) {
915                         timestamp.bv_len = STRLENOF("YYYYMMDDHHMMSSZ");
916                         AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
917                         timebuf[timestamp.bv_len-1] = 'Z';
918                         timebuf[timestamp.bv_len] = '\0';
919
920                 } else {
921                         time_t now = slap_get_time();
922
923                         timestamp.bv_len = sizeof(timebuf);
924
925                         slap_timestamp( &now, &timestamp );
926                 }
927
928                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
929                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
930                         nname = name;
931
932                 } else {
933                         name = op->o_dn;
934                         nname = op->o_ndn;
935                 }
936
937                 if ( !gotcsn ) {
938                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
939                         mod->sml_op = LDAP_MOD_REPLACE;
940                         mod->sml_flags = SLAP_MOD_INTERNAL;
941                         mod->sml_next = NULL;
942                         BER_BVZERO( &mod->sml_type );
943                         mod->sml_desc = slap_schema.si_ad_entryCSN;
944                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
945                         ber_dupbv( &mod->sml_values[0], &csn );
946                         BER_BVZERO( &mod->sml_values[1] );
947                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
948                         mod->sml_nvalues = NULL;
949                         *modtail = mod;
950                         modlast = mod;
951                         modtail = &mod->sml_next;
952                 }
953
954                 if ( !gotmname ) {
955                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
956                         mod->sml_op = LDAP_MOD_REPLACE;
957                         mod->sml_flags = SLAP_MOD_INTERNAL;
958                         mod->sml_next = NULL;
959                         BER_BVZERO( &mod->sml_type );
960                         mod->sml_desc = slap_schema.si_ad_modifiersName;
961                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
962                         ber_dupbv( &mod->sml_values[0], &name );
963                         BER_BVZERO( &mod->sml_values[1] );
964                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
965                         mod->sml_nvalues =
966                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
967                         ber_dupbv( &mod->sml_nvalues[0], &nname );
968                         BER_BVZERO( &mod->sml_nvalues[1] );
969                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
970                         *modtail = mod;
971                         modtail = &mod->sml_next;
972                 }
973
974                 if ( !gotmtime ) {
975                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
976                         mod->sml_op = LDAP_MOD_REPLACE;
977                         mod->sml_flags = SLAP_MOD_INTERNAL;
978                         mod->sml_next = NULL;
979                         BER_BVZERO( &mod->sml_type );
980                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
981                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
982                         ber_dupbv( &mod->sml_values[0], &timestamp );
983                         BER_BVZERO( &mod->sml_values[1] );
984                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
985                         mod->sml_nvalues = NULL;
986                         *modtail = mod;
987                         modtail = &mod->sml_next;
988                 }
989         }
990 }
991
992 int
993 slap_parse_modlist(
994         Operation *op,
995         SlapReply *rs,
996         BerElement *ber,
997         req_modify_s *ms )
998 {
999         ber_tag_t       tag;
1000         ber_len_t       len;
1001         char            *last;
1002         Modifications   **modtail = &ms->rs_modlist;
1003
1004         ms->rs_modlist = NULL;
1005         ms->rs_increment = 0;
1006
1007         rs->sr_err = LDAP_SUCCESS;
1008
1009         /* collect modifications & save for later */
1010         for ( tag = ber_first_element( ber, &len, &last );
1011                 tag != LBER_DEFAULT;
1012                 tag = ber_next_element( ber, &len, last ) )
1013         {
1014                 ber_int_t mop;
1015                 Modifications tmp, *mod;
1016
1017                 tmp.sml_nvalues = NULL;
1018
1019                 if ( ber_scanf( ber, "{e{m[W]}}", &mop,
1020                     &tmp.sml_type, &tmp.sml_values ) == LBER_ERROR )
1021                 {
1022                         rs->sr_text = "decoding modlist error";
1023                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1024                         goto done;
1025                 }
1026
1027                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
1028                 mod->sml_op = mop;
1029                 mod->sml_flags = 0;
1030                 mod->sml_type = tmp.sml_type;
1031                 mod->sml_values = tmp.sml_values;
1032                 mod->sml_nvalues = NULL;
1033                 mod->sml_desc = NULL;
1034                 mod->sml_next = NULL;
1035                 *modtail = mod;
1036
1037                 switch( mop ) {
1038                 case LDAP_MOD_ADD:
1039                         if ( mod->sml_values == NULL ) {
1040                                 Debug( LDAP_DEBUG_ANY, "slap_parse_modlist: "
1041                                         "modify/add operation (%ld) requires values\n",
1042                                         (long) mop, 0, 0 );
1043
1044                                 rs->sr_text = "modify/add operation requires values";
1045                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1046                                 goto done;
1047                         }
1048
1049                         /* fall through */
1050
1051                 case LDAP_MOD_DELETE:
1052                 case LDAP_MOD_REPLACE:
1053                         break;
1054
1055                 case LDAP_MOD_INCREMENT:
1056                         if( op->o_protocol >= LDAP_VERSION3 ) {
1057                                 ms->rs_increment++;
1058                                 if ( mod->sml_values == NULL ) {
1059                                         Debug( LDAP_DEBUG_ANY, "slap_parse_modlist: "
1060                                                 "modify/increment operation (%ld) requires value\n",
1061                                                 (long) mop, 0, 0 );
1062
1063                                         rs->sr_text = "modify/increment operation requires value";
1064                                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1065                                         goto done;
1066                                 }
1067
1068                                 if ( !BER_BVISNULL( &mod->sml_values[ 1 ] ) ) {
1069                                         Debug( LDAP_DEBUG_ANY,  "slap_parse_modlist: modify/increment "
1070                                                 "operation (%ld) requires single value\n",
1071                                                 (long) mop, 0, 0 );
1072
1073                                         rs->sr_text = "modify/increment operation requires single value";
1074                                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1075                                         goto done;
1076                                 }
1077
1078                                 break;
1079                         }
1080                         /* fall thru */
1081
1082                 default:
1083                         Debug( LDAP_DEBUG_ANY, "slap_parse_modlist: "
1084                                 "unrecognized modify operation (%ld)\n",
1085                                 (long) mop, 0, 0 );
1086
1087                         rs->sr_text = "unrecognized modify operation";
1088                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1089                         goto done;
1090                 }
1091
1092                 modtail = &mod->sml_next;
1093         }
1094         *modtail = NULL;
1095
1096 done:
1097         if ( rs->sr_err != LDAP_SUCCESS ) {
1098                 slap_mods_free( ms->rs_modlist, 1 );
1099                 ms->rs_modlist = NULL;
1100                 ms->rs_increment = 0;
1101         }
1102
1103         return rs->sr_err;
1104 }
1105