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