]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Fix prev commit, config_obsolete
[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
291                         op->o_bd = op_be;
292
293                         if ( !update ) {
294                                 rs->sr_err = slap_mods_no_user_mod_check( op, op->orm_modlist,
295                                         &rs->sr_text, textbuf, textlen );
296                                 if ( rs->sr_err != LDAP_SUCCESS ) {
297                                         send_ldap_result( op, rs );
298                                         goto cleanup;
299                                 }
300                         }
301                         op->o_bd->be_modify( op, rs );
302
303                 } else { /* send a referral */
304                         BerVarray defref = op->o_bd->be_update_refs
305                                 ? op->o_bd->be_update_refs : default_referral;
306                         if ( defref != NULL ) {
307                                 rs->sr_ref = referral_rewrite( defref,
308                                         NULL, &op->o_req_dn,
309                                         LDAP_SCOPE_DEFAULT );
310                                 if ( rs->sr_ref == NULL ) {
311                                         /* FIXME: must duplicate, because
312                                          * overlays may muck with it */
313                                         rs->sr_ref = defref;
314                                 }
315                                 rs->sr_err = LDAP_REFERRAL;
316                                 send_ldap_result( op, rs );
317                                 if ( rs->sr_ref != defref ) {
318                                         ber_bvarray_free( rs->sr_ref );
319                                 }
320
321                         } else {
322                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
323                                         "shadow context; no update referral" );
324                         }
325                 }
326
327         } else {
328                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
329                     "operation not supported within namingContext" );
330         }
331
332 cleanup:;
333         op->o_bd = bd;
334         return rs->sr_err;
335 }
336
337 /*
338  * Obsolete constraint checking.
339  */
340 int
341 slap_mods_obsolete_check(
342         Operation *op,
343         Modifications *ml,
344         const char **text,
345         char *textbuf,
346         size_t textlen )
347 {
348         if( get_relax( op ) ) return LDAP_SUCCESS;
349
350         for ( ; ml != NULL; ml = ml->sml_next ) {
351                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
352                         (( ml->sml_op != LDAP_MOD_REPLACE &&
353                                 ml->sml_op != LDAP_MOD_DELETE ) ||
354                                         ml->sml_values != NULL ))
355                 {
356                         /*
357                          * attribute is obsolete,
358                          * only allow replace/delete with no values
359                          */
360                         snprintf( textbuf, textlen,
361                                 "%s: attribute is obsolete",
362                                 ml->sml_type.bv_val );
363                         *text = textbuf;
364                         return LDAP_CONSTRAINT_VIOLATION;
365                 }
366         }
367
368         return LDAP_SUCCESS;
369 }
370
371 /*
372  * No-user-modification constraint checking.
373  */
374 int
375 slap_mods_no_user_mod_check(
376         Operation *op,
377         Modifications *ml,
378         const char **text,
379         char *textbuf,
380         size_t textlen )
381 {
382         for ( ; ml != NULL; ml = ml->sml_next ) {
383                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
384                         continue;
385                 }
386
387                 if ( get_relax( op ) ) {
388                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
389                                 ml->sml_flags |= SLAP_MOD_MANAGING;
390                                 continue;
391                         }
392
393                         /* attribute not manageable */
394                         snprintf( textbuf, textlen,
395                                 "%s: no-user-modification attribute not manageable",
396                                 ml->sml_type.bv_val );
397
398                 } else {
399                         /* user modification disallowed */
400                         snprintf( textbuf, textlen,
401                                 "%s: no user modification allowed",
402                                 ml->sml_type.bv_val );
403                 }
404
405                 *text = textbuf;
406                 return LDAP_CONSTRAINT_VIOLATION;
407         }
408
409         return LDAP_SUCCESS;
410 }
411
412 int
413 slap_mods_no_repl_user_mod_check(
414         Operation *op,
415         Modifications *ml,
416         const char **text,
417         char *textbuf,
418         size_t textlen )
419 {
420         Modifications *mods;
421         Modifications *modp;
422
423         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
424                 assert( mods->sml_op == LDAP_MOD_ADD );
425
426                 /* check doesn't already appear */
427                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
428                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
429                                 snprintf( textbuf, textlen,
430                                         "attribute '%s' provided more than once",
431                                         mods->sml_desc->ad_cname.bv_val );
432                                 *text = textbuf;
433                                 return LDAP_TYPE_OR_VALUE_EXISTS;
434                         }
435                 }
436         }
437
438         return LDAP_SUCCESS;
439 }
440
441 /*
442  * Do basic attribute type checking and syntax validation.
443  */
444 int slap_mods_check(
445         Operation *op,
446         Modifications *ml,
447         const char **text,
448         char *textbuf,
449         size_t textlen,
450         void *ctx )
451 {
452         int rc;
453
454         for( ; ml != NULL; ml = ml->sml_next ) {
455                 AttributeDescription *ad = NULL;
456
457                 /* convert to attribute description */
458                 if ( ml->sml_desc == NULL ) {
459                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
460                         if( rc != LDAP_SUCCESS ) {
461                                 if ( get_no_schema_check( op )) {
462                                         rc = slap_bv2undef_ad( &ml->sml_type, &ml->sml_desc,
463                                                 text, 0 );
464                                 }
465                         }
466                         if( rc != LDAP_SUCCESS ) {
467                                 snprintf( textbuf, textlen, "%s: %s",
468                                         ml->sml_type.bv_val, *text );
469                                 *text = textbuf;
470                                 return rc;
471                         }
472                 }
473
474                 ad = ml->sml_desc;
475
476                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
477                         && !slap_ad_is_binary( ad ))
478                 {
479                         /* attribute requires binary transfer */
480                         snprintf( textbuf, textlen,
481                                 "%s: requires ;binary transfer",
482                                 ml->sml_type.bv_val );
483                         *text = textbuf;
484                         return LDAP_UNDEFINED_TYPE;
485                 }
486
487                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
488                         && slap_ad_is_binary( ad ))
489                 {
490                         /* attribute does not require binary transfer */
491                         snprintf( textbuf, textlen,
492                                 "%s: disallows ;binary transfer",
493                                 ml->sml_type.bv_val );
494                         *text = textbuf;
495                         return LDAP_UNDEFINED_TYPE;
496                 }
497
498                 if( slap_ad_is_tag_range( ad )) {
499                         /* attribute requires binary transfer */
500                         snprintf( textbuf, textlen,
501                                 "%s: inappropriate use of tag range option",
502                                 ml->sml_type.bv_val );
503                         *text = textbuf;
504                         return LDAP_UNDEFINED_TYPE;
505                 }
506
507 #if 0
508                 if ( is_at_obsolete( ad->ad_type ) &&
509                         (( ml->sml_op != LDAP_MOD_REPLACE &&
510                                 ml->sml_op != LDAP_MOD_DELETE ) ||
511                                         ml->sml_values != NULL ))
512                 {
513                         /*
514                          * attribute is obsolete,
515                          * only allow replace/delete with no values
516                          */
517                         snprintf( textbuf, textlen,
518                                 "%s: attribute is obsolete",
519                                 ml->sml_type.bv_val );
520                         *text = textbuf;
521                         return LDAP_CONSTRAINT_VIOLATION;
522                 }
523 #endif
524
525                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
526 #ifdef SLAPD_REAL_SYNTAX
527                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
528 #endif
529                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
530                 {
531                         /*
532                          * attribute values must be INTEGER or REAL
533                          */
534                         snprintf( textbuf, textlen,
535                                 "%s: attribute syntax inappropriate for increment",
536                                 ml->sml_type.bv_val );
537                         *text = textbuf;
538                         return LDAP_CONSTRAINT_VIOLATION;
539                 }
540
541                 /*
542                  * check values
543                  */
544                 if( ml->sml_values != NULL ) {
545                         ber_len_t nvals;
546                         slap_syntax_validate_func *validate =
547                                 ad->ad_type->sat_syntax->ssyn_validate;
548                         slap_syntax_transform_func *pretty =
549                                 ad->ad_type->sat_syntax->ssyn_pretty;
550  
551                         if( !pretty && !validate ) {
552                                 *text = "no validator for syntax";
553                                 snprintf( textbuf, textlen,
554                                         "%s: no validator for syntax %s",
555                                         ml->sml_type.bv_val,
556                                         ad->ad_type->sat_syntax->ssyn_oid );
557                                 *text = textbuf;
558                                 return LDAP_INVALID_SYNTAX;
559                         }
560
561                         /*
562                          * check that each value is valid per syntax
563                          *      and pretty if appropriate
564                          */
565                         for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
566                                 struct berval pval;
567
568                                 if ( pretty ) {
569                                         rc = ordered_value_pretty( ad,
570                                                 &ml->sml_values[nvals], &pval, ctx );
571                                 } else {
572                                         rc = ordered_value_validate( ad,
573                                                 &ml->sml_values[nvals], ml->sml_op );
574                                 }
575
576                                 if( rc != 0 ) {
577                                         snprintf( textbuf, textlen,
578                                                 "%s: value #%ld invalid per syntax",
579                                                 ml->sml_type.bv_val, (long) nvals );
580                                         *text = textbuf;
581                                         return LDAP_INVALID_SYNTAX;
582                                 }
583
584                                 if( pretty ) {
585                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
586                                         ml->sml_values[nvals] = pval;
587                                 }
588                         }
589
590                         /*
591                          * a rough single value check... an additional check is needed
592                          * to catch add of single value to existing single valued attribute
593                          */
594                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
595                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
596                         {
597                                 snprintf( textbuf, textlen,
598                                         "%s: multiple values provided",
599                                         ml->sml_type.bv_val );
600                                 *text = textbuf;
601                                 return LDAP_CONSTRAINT_VIOLATION;
602                         }
603
604                         /* if the type has a normalizer, generate the
605                          * normalized values. otherwise leave them NULL.
606                          *
607                          * this is different from the rule for attributes
608                          * in an entry - in an attribute list, the normalized
609                          * value is set equal to the non-normalized value
610                          * when there is no normalizer.
611                          */
612                         if( nvals && ad->ad_type->sat_equality &&
613                                 ad->ad_type->sat_equality->smr_normalize )
614                         {
615                                 ml->sml_nvalues = ber_memalloc_x(
616                                         (nvals+1)*sizeof(struct berval), ctx );
617
618                                 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
619                                         rc = ordered_value_normalize(
620                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
621                                                 ad,
622                                                 ad->ad_type->sat_equality,
623                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
624                                         if ( rc ) {
625                                                 Debug( LDAP_DEBUG_ANY,
626                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
627                                                         rc, 0, 0 );
628                                                 snprintf( textbuf, textlen,
629                                                         "%s: value #%ld normalization failed",
630                                                         ml->sml_type.bv_val, (long) nvals );
631                                                 *text = textbuf;
632                                                 return rc;
633                                         }
634                                 }
635
636                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
637                         }
638
639                         /* check for duplicates, but ignore Deletes.
640                          */
641                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
642 #define SLAP_MODS_CHECK_QUICKSORT
643 #ifndef SLAP_MODS_CHECK_QUICKSORT
644                                 int             i, j, rc, match;
645                                 MatchingRule *mr = ad->ad_type->sat_equality;
646
647                                 for ( i = 1; i < nvals ; i++ ) {
648                                         /* test asserted values against themselves */
649                                         for( j = 0; j < i; j++ ) {
650                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
651                                                         SLAP_MR_EQUALITY
652                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
653                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
654                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
655                                                         ml->sml_nvalues
656                                                                 ? &ml->sml_nvalues[i]
657                                                                 : &ml->sml_values[i],
658                                                         ml->sml_nvalues
659                                                                 ? &ml->sml_nvalues[j]
660                                                                 : &ml->sml_values[j],
661                                                         text );
662                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
663                                                         /* value exists already */
664                                                         snprintf( textbuf, textlen,
665                                                                 "%s: value #%d provided more than once",
666                                                                 ml->sml_desc->ad_cname.bv_val, j );
667                                                         *text = textbuf;
668                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
669
670                                                 } else if ( rc != LDAP_SUCCESS ) {
671                                                         return rc;
672                                                 }
673                                         }
674                                 }
675 #else   /* SLAP_MODS_CHECK_QUICKSORT */
676
677 /* Quicksort + Insertion sort for small arrays */
678
679 #define SMALL   8
680 #define SWAP(a,b,tmp)   tmp=(a);(a)=(b);(b)=tmp
681 #define COMP(a,b)       match=0; rc = ordered_value_match( &match, \
682                                                 ml->sml_desc, mr, SLAP_MR_EQUALITY \
683                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX \
684                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH \
685                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH, \
686                                                                 &(a), &(b), text );
687
688                                         MatchingRule *mr = ad->ad_type->sat_equality;
689                                         int istack[sizeof(int)*16];
690                                         int i, j, k, l, ir, jstack, match, *ix, itmp;
691                                         struct berval a, *cv;
692
693 /* If PRESERVE_ORDER is defined only the index array is sorted; the
694  * actual values are left in their incoming order. Otherwise, the
695  * only reason to keep the index array is to identify the offending
696  * value when duplicates are found.
697  */
698 #define PRESERVE_ORDER
699 #ifndef PRESERVE_ORDER
700                                         struct berval va, *v, *nv, bvtmp;
701
702 #define IX(x)   x
703 #define EXCH(x,y)       SWAP(ix[x],ix[y],itmp); SWAP(cv[x],cv[y],bvtmp); \
704         if (nv) {SWAP(v[x],v[y],bvtmp);}
705 #define SETA(x) itmp = ix[x]; a = cv[x]; if (nv) va=v[x]
706 #define GETA(x) ix[x] = itmp; cv[x] = a; if (nv) v[x]=va
707 #define SET(x,y)        ix[x] = ix[y]; cv[x] = cv[y]; if (nv) v[x]=v[y]
708
709                                         v = ml->sml_values;
710                                         nv = ml->sml_nvalues;
711
712 #else   /* PRESERVE_ORDER */
713
714 #define IX(x)   ix[x]
715 #define EXCH(x,y)       SWAP(ix[x],ix[y],itmp)
716 #define SETA(x) itmp = ix[x]; a = cv[itmp]
717 #define GETA(x) ix[x] = itmp;
718 #define SET(x,y)        ix[x] = ix[y]
719
720 #endif  /* PRESERVE_ORDER */
721
722                                         cv = ml->sml_nvalues ? ml->sml_nvalues : ml->sml_values;
723                                         if ( ad == slap_schema.si_ad_objectClass )
724                                                 mr = NULL;      /* shortcut matching */
725
726                                         /* record indices to preserve input ordering */
727                                         ix = slap_sl_malloc( nvals * sizeof(int), ctx );
728                                         for (i=0; i<nvals; i++) ix[i] = i;
729
730                                         ir = nvals-1;
731                                         l = 0;
732                                         jstack = 0;
733
734                                         for(;;) {
735                                                 if (ir - l < SMALL) {   /* Insertion sort */
736                                                         match=1;
737                                                         for (j=l+1;j<=ir;j++) {
738                                                                 SETA(j);
739                                                                 for (i=j-1;i>=0;i--) {
740                                                                         COMP(cv[IX(i)], a);
741                                                                         if ( match <= 0 )
742                                                                                 break;
743                                                                         SET(i+1,i);
744                                                                 }
745                                                                 GETA(i+1);
746                                                                 if ( match == 0 ) goto done;
747                                                         }
748                                                         if ( jstack == 0 ) break;
749                                                         if ( match == 0 ) break;
750                                                         ir = istack[jstack--];
751                                                         l = istack[jstack--];
752                                                 } else {
753                                                         k = (l + ir) >> 1;      /* Choose median of left, center, right */
754                                                         EXCH(k, l+1);
755                                                         COMP( cv[IX(l)], cv[IX(ir)] );
756                                                         if ( match > 0 ) {
757                                                                 EXCH(l, ir);
758                                                         } else if ( match == 0 ) {
759                                                                 i = ir;
760                                                                 break;
761                                                         }
762                                                         COMP( cv[IX(l+1)], cv[IX(ir)] );
763                                                         if ( match > 0 ) {
764                                                                 EXCH(l+1, ir);
765                                                         } else if ( match == 0 ) {
766                                                                 i = ir;
767                                                                 break;
768                                                         }
769                                                         COMP( cv[IX(l)], cv[IX(l+1)] );
770                                                         if ( match > 0 ) {
771                                                                 EXCH(l, l+1);
772                                                         } else if ( match == 0 ) {
773                                                                 i = l;
774                                                                 break;
775                                                         }
776                                                         i = l+1;
777                                                         j = ir;
778                                                         a = cv[IX(i)];
779                                                         for(;;) {
780                                                                 do {
781                                                                         i++;
782                                                                         COMP( cv[IX(i)], a );
783                                                                 } while( match < 0 );
784                                                                 while( match > 0 ) {
785                                                                         j--;
786                                                                         COMP( cv[IX(j)], a );
787                                                                 }
788                                                                 if (j < i) {
789                                                                         match = 1;
790                                                                         break;
791                                                                 }
792                                                                 if ( match == 0 ) {
793                                                                         i = l+1;
794                                                                         break;
795                                                                 }
796                                                                 EXCH(i,j);
797                                                         }
798                                                         if ( match == 0 )
799                                                                 break;
800                                                         EXCH(l+1,j);
801                                                         jstack += 2;
802                                                         if (ir-i+1 >= j) {
803                                                                 istack[jstack] = ir;
804                                                                 istack[jstack-1] = i;
805                                                                 ir = j;
806                                                         } else {
807                                                                 istack[jstack] = j;
808                                                                 istack[jstack-1] = l;
809                                                                 l = i;
810                                                 }
811                                         }
812                                 }
813 done:
814                                 if ( i >= 0 )
815                                         j = ix[i];
816
817                                 slap_sl_free( ix, ctx );
818
819                                 if ( rc != LDAP_SUCCESS ) {
820                                         return rc;
821                                 } else if ( match == 0 ) {
822                                         /* value exists already */
823                                         assert( i >= 0 );
824                                         assert( i < nvals );
825                                         snprintf( textbuf, textlen,
826                                                 "%s: value #%d provided more than once",
827                                                 ml->sml_desc->ad_cname.bv_val, j );
828                                         *text = textbuf;
829                                         return LDAP_TYPE_OR_VALUE_EXISTS;
830                                 }
831 #endif  /* SLAP_MODS_CHECK_QUICKSORT */
832                         }
833                 }
834         }
835
836         return LDAP_SUCCESS;
837 }
838
839 /* Enter with bv->bv_len = sizeof buffer, returns with
840  * actual length of string
841  */
842 void slap_timestamp( time_t *tm, struct berval *bv )
843 {
844         struct tm *ltm;
845 #ifdef HAVE_GMTIME_R
846         struct tm ltm_buf;
847
848         ltm = gmtime_r( tm, &ltm_buf );
849 #else
850         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
851         ltm = gmtime( tm );
852 #endif
853
854         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
855
856 #ifndef HAVE_GMTIME_R
857         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
858 #endif
859 }
860
861 /* Called for all modify and modrdn ops. If the current op was replicated
862  * from elsewhere, all of the attrs should already be present.
863  */
864 void slap_mods_opattrs(
865         Operation *op,
866         Modifications **modsp,
867         int manage_ctxcsn )
868 {
869         struct berval name, timestamp, csn = BER_BVNULL;
870         struct berval nname;
871         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
872         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
873         Modifications *mod, **modtail, *modlast;
874         int gotcsn = 0, gotmname = 0, gotmtime = 0;
875
876         if ( SLAP_LASTMOD( op->o_bd ) && !op->orm_no_opattrs ) {
877                 char *ptr;
878                 timestamp.bv_val = timebuf;
879                 for ( modtail = modsp; *modtail; modtail = &(*modtail)->sml_next ) {
880                         if ( (*modtail)->sml_op != LDAP_MOD_ADD &&
881                                 (*modtail)->sml_op != LDAP_MOD_REPLACE )
882                         {
883                                 continue;
884                         }
885
886                         if ( (*modtail)->sml_desc == slap_schema.si_ad_entryCSN )
887                         {
888                                 csn = (*modtail)->sml_values[0];
889                                 gotcsn = 1;
890
891                         } else if ( (*modtail)->sml_desc == slap_schema.si_ad_modifiersName )
892                         {
893                                 gotmname = 1;
894
895                         } else if ( (*modtail)->sml_desc == slap_schema.si_ad_modifyTimestamp )
896                         {
897                                 gotmtime = 1;
898                         }
899                 }
900
901                 if ( BER_BVISEMPTY( &op->o_csn )) {
902                         if ( !gotcsn ) {
903                                 csn.bv_val = csnbuf;
904                                 csn.bv_len = sizeof( csnbuf );
905                                 slap_get_csn( op, &csn, manage_ctxcsn );
906
907                         } else {
908                                 if ( manage_ctxcsn ) {
909                                         slap_queue_csn( op, &csn );
910                                 }
911                         }
912
913                 } else {
914                         csn = op->o_csn;
915                 }
916
917                 ptr = ber_bvchr( &csn, '#' );
918                 if ( ptr ) {
919                         timestamp.bv_len = STRLENOF("YYYYMMDDHHMMSSZ");
920                         AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
921                         timebuf[timestamp.bv_len-1] = 'Z';
922                         timebuf[timestamp.bv_len] = '\0';
923
924                 } else {
925                         time_t now = slap_get_time();
926
927                         timestamp.bv_len = sizeof(timebuf);
928
929                         slap_timestamp( &now, &timestamp );
930                 }
931
932                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
933                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
934                         nname = name;
935
936                 } else {
937                         name = op->o_dn;
938                         nname = op->o_ndn;
939                 }
940
941                 if ( !gotcsn ) {
942                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
943                         mod->sml_op = LDAP_MOD_REPLACE;
944                         mod->sml_flags = SLAP_MOD_INTERNAL;
945                         mod->sml_next = NULL;
946                         BER_BVZERO( &mod->sml_type );
947                         mod->sml_desc = slap_schema.si_ad_entryCSN;
948                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
949                         ber_dupbv( &mod->sml_values[0], &csn );
950                         BER_BVZERO( &mod->sml_values[1] );
951                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
952                         mod->sml_nvalues = NULL;
953                         *modtail = mod;
954                         modlast = mod;
955                         modtail = &mod->sml_next;
956                 }
957
958                 if ( !gotmname ) {
959                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
960                         mod->sml_op = LDAP_MOD_REPLACE;
961                         mod->sml_flags = SLAP_MOD_INTERNAL;
962                         mod->sml_next = NULL;
963                         BER_BVZERO( &mod->sml_type );
964                         mod->sml_desc = slap_schema.si_ad_modifiersName;
965                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
966                         ber_dupbv( &mod->sml_values[0], &name );
967                         BER_BVZERO( &mod->sml_values[1] );
968                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
969                         mod->sml_nvalues =
970                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
971                         ber_dupbv( &mod->sml_nvalues[0], &nname );
972                         BER_BVZERO( &mod->sml_nvalues[1] );
973                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
974                         *modtail = mod;
975                         modtail = &mod->sml_next;
976                 }
977
978                 if ( !gotmtime ) {
979                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
980                         mod->sml_op = LDAP_MOD_REPLACE;
981                         mod->sml_flags = SLAP_MOD_INTERNAL;
982                         mod->sml_next = NULL;
983                         BER_BVZERO( &mod->sml_type );
984                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
985                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
986                         ber_dupbv( &mod->sml_values[0], &timestamp );
987                         BER_BVZERO( &mod->sml_values[1] );
988                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
989                         mod->sml_nvalues = NULL;
990                         *modtail = mod;
991                         modtail = &mod->sml_next;
992                 }
993         }
994 }
995
996 int
997 slap_parse_modlist(
998         Operation *op,
999         SlapReply *rs,
1000         BerElement *ber,
1001         req_modify_s *ms )
1002 {
1003         ber_tag_t       tag;
1004         ber_len_t       len;
1005         char            *last;
1006         Modifications   **modtail = &ms->rs_modlist;
1007
1008         ms->rs_modlist = NULL;
1009         ms->rs_increment = 0;
1010
1011         rs->sr_err = LDAP_SUCCESS;
1012
1013         /* collect modifications & save for later */
1014         for ( tag = ber_first_element( ber, &len, &last );
1015                 tag != LBER_DEFAULT;
1016                 tag = ber_next_element( ber, &len, last ) )
1017         {
1018                 ber_int_t mop;
1019                 Modifications tmp, *mod;
1020
1021                 tmp.sml_nvalues = NULL;
1022
1023                 if ( ber_scanf( ber, "{e{m[W]}}", &mop,
1024                     &tmp.sml_type, &tmp.sml_values ) == LBER_ERROR )
1025                 {
1026                         rs->sr_text = "decoding modlist error";
1027                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1028                         goto done;
1029                 }
1030
1031                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
1032                 mod->sml_op = mop;
1033                 mod->sml_flags = 0;
1034                 mod->sml_type = tmp.sml_type;
1035                 mod->sml_values = tmp.sml_values;
1036                 mod->sml_nvalues = NULL;
1037                 mod->sml_desc = NULL;
1038                 mod->sml_next = NULL;
1039                 *modtail = mod;
1040
1041                 switch( mop ) {
1042                 case LDAP_MOD_ADD:
1043                         if ( mod->sml_values == NULL ) {
1044                                 Debug( LDAP_DEBUG_ANY, "slap_parse_modlist: "
1045                                         "modify/add operation (%ld) requires values\n",
1046                                         (long) mop, 0, 0 );
1047
1048                                 rs->sr_text = "modify/add operation requires values";
1049                                 rs->sr_err = LDAP_PROTOCOL_ERROR;
1050                                 goto done;
1051                         }
1052
1053                         /* fall through */
1054
1055                 case LDAP_MOD_DELETE:
1056                 case LDAP_MOD_REPLACE:
1057                         break;
1058
1059                 case LDAP_MOD_INCREMENT:
1060                         if( op->o_protocol >= LDAP_VERSION3 ) {
1061                                 ms->rs_increment++;
1062                                 if ( mod->sml_values == NULL ) {
1063                                         Debug( LDAP_DEBUG_ANY, "slap_parse_modlist: "
1064                                                 "modify/increment operation (%ld) requires value\n",
1065                                                 (long) mop, 0, 0 );
1066
1067                                         rs->sr_text = "modify/increment operation requires value";
1068                                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1069                                         goto done;
1070                                 }
1071
1072                                 if ( !BER_BVISNULL( &mod->sml_values[ 1 ] ) ) {
1073                                         Debug( LDAP_DEBUG_ANY,  "slap_parse_modlist: modify/increment "
1074                                                 "operation (%ld) requires single value\n",
1075                                                 (long) mop, 0, 0 );
1076
1077                                         rs->sr_text = "modify/increment operation requires single value";
1078                                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1079                                         goto done;
1080                                 }
1081
1082                                 break;
1083                         }
1084                         /* fall thru */
1085
1086                 default:
1087                         Debug( LDAP_DEBUG_ANY, "slap_parse_modlist: "
1088                                 "unrecognized modify operation (%ld)\n",
1089                                 (long) mop, 0, 0 );
1090
1091                         rs->sr_text = "unrecognized modify operation";
1092                         rs->sr_err = LDAP_PROTOCOL_ERROR;
1093                         goto done;
1094                 }
1095
1096                 modtail = &mod->sml_next;
1097         }
1098         *modtail = NULL;
1099
1100 done:
1101         if ( rs->sr_err != LDAP_SUCCESS ) {
1102                 slap_mods_free( ms->rs_modlist, 1 );
1103                 ms->rs_modlist = NULL;
1104                 ms->rs_increment = 0;
1105         }
1106
1107         return rs->sr_err;
1108 }
1109