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