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