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