]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
manageDIT framework and obsolete objectclasses/DIT-content-rule override
[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                         if ( !repl_user ) {
446                                 for( modtail = &modlist;
447                                         *modtail != NULL;
448                                         modtail = &(*modtail)->sml_next )
449                                 {
450                                         /* empty */
451                                 }
452
453                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
454                                         &rs->sr_text, textbuf, textlen, 1 );
455                                 if( rs->sr_err != LDAP_SUCCESS ) {
456                                         send_ldap_result( op, rs );
457                                         goto cleanup;
458                                 }
459                         }
460
461                         op->orm_modlist = modlist;
462 #ifdef SLAPD_MULTIMASTER
463                         if ( !repl_user )
464 #endif
465                         {
466                                 /* but multimaster slapd logs only the ones 
467                                  * not from a replicator user */
468                                 cb.sc_next = op->o_callback;
469                                 op->o_callback = &cb;
470                         }
471                         op->o_bd->be_modify( op, rs );
472
473 #ifndef SLAPD_MULTIMASTER
474                 /* send a referral */
475                 } else {
476                         BerVarray defref = op->o_bd->be_update_refs
477                                 ? op->o_bd->be_update_refs : default_referral;
478                         if ( defref != NULL ) {
479                                 rs->sr_ref = referral_rewrite( defref,
480                                         NULL, &op->o_req_dn,
481                                         LDAP_SCOPE_DEFAULT );
482                                 if ( rs->sr_ref == NULL ) {
483                                         /* FIXME: must duplicate, because
484                                          * overlays may muck with it */
485                                         rs->sr_ref = defref;
486                                 }
487                                 rs->sr_err = LDAP_REFERRAL;
488                                 send_ldap_result( op, rs );
489                                 if ( rs->sr_ref != defref ) {
490                                         ber_bvarray_free( rs->sr_ref );
491                                 }
492
493                         } else {
494                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
495                                         "shadow context; no update referral" );
496                         }
497 #endif
498                 }
499         } else {
500                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
501                     "operation not supported within namingContext" );
502         }
503
504 #if defined( LDAP_SLAPI )
505         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
506                 SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 )
507         {
508                 Debug(LDAP_DEBUG_TRACE,
509                         "do_modify: modify postoperation plugins failed.\n", 0, 0, 0);
510         }
511 #endif /* defined( LDAP_SLAPI ) */
512
513 cleanup:;
514 #if defined( LDAP_SLAPI )
515         if ( modv != NULL ) slapi_int_free_ldapmods( modv );
516 #endif
517
518         return rs->sr_err;
519 }
520
521 /*
522  * Do non-update constraint checking.
523  */
524 int
525 slap_mods_no_update_check(
526         Modifications *ml,
527         const char **text,
528         char *textbuf,
529         size_t textlen )
530 {
531         for ( ; ml != NULL; ml = ml->sml_next ) {
532                 if ( is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
533                         /* user modification disallowed */
534                         snprintf( textbuf, textlen,
535                                 "%s: no user modification allowed",
536                                 ml->sml_type.bv_val );
537                         *text = textbuf;
538                         return LDAP_CONSTRAINT_VIOLATION;
539                 }
540         }
541
542         return LDAP_SUCCESS;
543 }
544
545 /*
546  * Do basic attribute type checking and syntax validation.
547  */
548 int slap_mods_check(
549         Modifications *ml,
550         const char **text,
551         char *textbuf,
552         size_t textlen,
553         void *ctx )
554 {
555         int rc;
556
557         for( ; ml != NULL; ml = ml->sml_next ) {
558                 AttributeDescription *ad = NULL;
559
560                 /* convert to attribute description */
561                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
562
563                 if( rc != LDAP_SUCCESS ) {
564                         snprintf( textbuf, textlen, "%s: %s",
565                                 ml->sml_type.bv_val, *text );
566                         *text = textbuf;
567                         return rc;
568                 }
569
570                 ad = ml->sml_desc;
571
572                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
573                         && !slap_ad_is_binary( ad ))
574                 {
575                         /* attribute requires binary transfer */
576                         snprintf( textbuf, textlen,
577                                 "%s: requires ;binary transfer",
578                                 ml->sml_type.bv_val );
579                         *text = textbuf;
580                         return LDAP_UNDEFINED_TYPE;
581                 }
582
583                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
584                         && slap_ad_is_binary( ad ))
585                 {
586                         /* attribute does not require binary transfer */
587                         snprintf( textbuf, textlen,
588                                 "%s: disallows ;binary transfer",
589                                 ml->sml_type.bv_val );
590                         *text = textbuf;
591                         return LDAP_UNDEFINED_TYPE;
592                 }
593
594                 if( slap_ad_is_tag_range( ad )) {
595                         /* attribute requires binary transfer */
596                         snprintf( textbuf, textlen,
597                                 "%s: inappropriate use of tag range option",
598                                 ml->sml_type.bv_val );
599                         *text = textbuf;
600                         return LDAP_UNDEFINED_TYPE;
601                 }
602
603 #if 0
604                 /* moved to slap_mods_no_update_check() */
605                 if (!update && is_at_no_user_mod( ad->ad_type )) {
606                         /* user modification disallowed */
607                         snprintf( textbuf, textlen,
608                                 "%s: no user modification allowed",
609                                 ml->sml_type.bv_val );
610                         *text = textbuf;
611                         return LDAP_CONSTRAINT_VIOLATION;
612                 }
613 #endif
614
615                 if ( is_at_obsolete( ad->ad_type ) &&
616                         (( ml->sml_op != LDAP_MOD_REPLACE &&
617                                 ml->sml_op != LDAP_MOD_DELETE ) ||
618                                         ml->sml_values != NULL ))
619                 {
620                         /*
621                          * attribute is obsolete,
622                          * only allow replace/delete with no values
623                          */
624                         snprintf( textbuf, textlen,
625                                 "%s: attribute is obsolete",
626                                 ml->sml_type.bv_val );
627                         *text = textbuf;
628                         return LDAP_CONSTRAINT_VIOLATION;
629                 }
630
631                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
632 #ifdef SLAPD_REAL_SYNTAX
633                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
634 #endif
635                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
636                 {
637                         /*
638                          * attribute values must be INTEGER or REAL
639                          */
640                         snprintf( textbuf, textlen,
641                                 "%s: attribute syntax inappropriate for increment",
642                                 ml->sml_type.bv_val );
643                         *text = textbuf;
644                         return LDAP_CONSTRAINT_VIOLATION;
645                 }
646
647                 /*
648                  * check values
649                  */
650                 if( ml->sml_values != NULL ) {
651                         ber_len_t nvals;
652                         slap_syntax_validate_func *validate =
653                                 ad->ad_type->sat_syntax->ssyn_validate;
654                         slap_syntax_transform_func *pretty =
655                                 ad->ad_type->sat_syntax->ssyn_pretty;
656  
657                         if( !pretty && !validate ) {
658                                 *text = "no validator for syntax";
659                                 snprintf( textbuf, textlen,
660                                         "%s: no validator for syntax %s",
661                                         ml->sml_type.bv_val,
662                                         ad->ad_type->sat_syntax->ssyn_oid );
663                                 *text = textbuf;
664                                 return LDAP_INVALID_SYNTAX;
665                         }
666
667                         /*
668                          * check that each value is valid per syntax
669                          *      and pretty if appropriate
670                          */
671                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
672                                 struct berval pval;
673                                 if( pretty ) {
674                                         rc = pretty( ad->ad_type->sat_syntax,
675                                                 &ml->sml_values[nvals], &pval, ctx );
676                                 } else {
677                                         rc = validate( ad->ad_type->sat_syntax,
678                                                 &ml->sml_values[nvals] );
679                                 }
680
681                                 if( rc != 0 ) {
682                                         snprintf( textbuf, textlen,
683                                                 "%s: value #%ld invalid per syntax",
684                                                 ml->sml_type.bv_val, (long) nvals );
685                                         *text = textbuf;
686                                         return LDAP_INVALID_SYNTAX;
687                                 }
688
689                                 if( pretty ) {
690                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
691                                         ml->sml_values[nvals] = pval;
692                                 }
693                         }
694
695                         /*
696                          * a rough single value check... an additional check is needed
697                          * to catch add of single value to existing single valued attribute
698                          */
699                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
700                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
701                         {
702                                 snprintf( textbuf, textlen,
703                                         "%s: multiple values provided",
704                                         ml->sml_type.bv_val );
705                                 *text = textbuf;
706                                 return LDAP_CONSTRAINT_VIOLATION;
707                         }
708
709                         /* if the type has a normalizer, generate the
710                          * normalized values. otherwise leave them NULL.
711                          *
712                          * this is different from the rule for attributes
713                          * in an entry - in an attribute list, the normalized
714                          * value is set equal to the non-normalized value
715                          * when there is no normalizer.
716                          */
717                         if( nvals && ad->ad_type->sat_equality &&
718                                 ad->ad_type->sat_equality->smr_normalize )
719                         {
720                                 ml->sml_nvalues = ber_memalloc_x(
721                                         (nvals+1)*sizeof(struct berval), ctx );
722
723                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
724                                         rc = ad->ad_type->sat_equality->smr_normalize(
725                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
726                                                 ad->ad_type->sat_syntax,
727                                                 ad->ad_type->sat_equality,
728                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
729                                         if( rc ) {
730                                                 Debug( LDAP_DEBUG_ANY,
731                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
732                                                         rc, 0, 0 );
733                                                 snprintf( textbuf, textlen,
734                                                         "%s: value #%ld normalization failed",
735                                                         ml->sml_type.bv_val, (long) nvals );
736                                                 *text = textbuf;
737                                                 return rc;
738                                         }
739                                 }
740
741                                 ml->sml_nvalues[nvals].bv_val = NULL;
742                                 ml->sml_nvalues[nvals].bv_len = 0;
743                         }
744
745                         /* check for duplicates, but ignore Deletes.
746                          */
747                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
748                                 int             i, j, rc, match;
749                                 MatchingRule *mr = ad->ad_type->sat_equality;
750
751                                 for ( i = 1; i < nvals ; i++ ) {
752                                         /* test asserted values against themselves */
753                                         for( j = 0; j < i; j++ ) {
754                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
755                                                         SLAP_MR_EQUALITY
756                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
757                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
758                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
759                                                         ml->sml_nvalues
760                                                                 ? &ml->sml_nvalues[i]
761                                                                 : &ml->sml_values[i],
762                                                         ml->sml_nvalues
763                                                                 ? &ml->sml_nvalues[j]
764                                                                 : &ml->sml_values[j],
765                                                         text );
766                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
767                                                         /* value exists already */
768                                                         snprintf( textbuf, textlen,
769                                                                 "%s: value #%d provided more than once",
770                                                                 ml->sml_desc->ad_cname.bv_val, j );
771                                                         *text = textbuf;
772                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
773
774                                                 } else if ( rc != LDAP_SUCCESS ) {
775                                                         return rc;
776                                                 }
777                                         }
778                                 }
779                         }
780
781                 }
782         }
783
784         return LDAP_SUCCESS;
785 }
786
787 int slap_mods_opattrs(
788         Operation *op,
789         Modifications *mods,
790         Modifications **modtail,
791         const char **text,
792         char *textbuf, size_t textlen,
793         int manage_ctxcsn )
794 {
795         struct berval name, timestamp, csn;
796         struct berval nname;
797         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
798         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
799         Modifications *mod;
800
801         int mop = op->o_tag == LDAP_REQ_ADD
802                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
803
804         assert( modtail != NULL );
805         assert( *modtail == NULL );
806
807         if ( SLAP_LASTMOD( op->o_bd )) {
808                 struct tm *ltm;
809 #ifdef HAVE_GMTIME_R
810                 struct tm ltm_buf;
811 #endif
812                 time_t now = slap_get_time();
813
814 #ifdef HAVE_GMTIME_R
815                 ltm = gmtime_r( &now, &ltm_buf );
816 #else
817                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
818                 ltm = gmtime( &now );
819 #endif /* HAVE_GMTIME_R */
820                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
821
822                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, manage_ctxcsn );
823
824 #ifndef HAVE_GMTIME_R
825                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
826 #endif
827
828                 timestamp.bv_val = timebuf;
829                 timestamp.bv_len = strlen(timebuf);
830
831                 if( op->o_dn.bv_len == 0 ) {
832                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
833                         nname = name;
834                 } else {
835                         name = op->o_dn;
836                         nname = op->o_ndn;
837                 }
838         }
839
840         if( op->o_tag == LDAP_REQ_ADD ) {
841                 struct berval tmpval;
842
843                 {
844                         int rc = mods_structural_class( mods, &tmpval,
845                                 text, textbuf, textlen );
846                         if( rc != LDAP_SUCCESS ) return rc;
847
848                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
849                         mod->sml_op = mop;
850                         mod->sml_type.bv_val = NULL;
851                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
852                         mod->sml_values =
853                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
854                         ber_dupbv( &mod->sml_values[0], &tmpval );
855                         mod->sml_values[1].bv_len = 0;
856                         mod->sml_values[1].bv_val = NULL;
857                         assert( mod->sml_values[0].bv_val );
858                         mod->sml_nvalues =
859                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
860                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
861                         mod->sml_nvalues[1].bv_len = 0;
862                         mod->sml_nvalues[1].bv_val = NULL;
863                         assert( mod->sml_nvalues[0].bv_val );
864                         *modtail = mod;
865                         modtail = &mod->sml_next;
866                 }
867
868                 if ( SLAP_LASTMOD( op->o_bd )) {
869                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
870
871                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
872                         tmpval.bv_val = uuidbuf;
873                 
874                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
875                         mod->sml_op = mop;
876                         mod->sml_type.bv_val = NULL;
877                         mod->sml_desc = slap_schema.si_ad_entryUUID;
878                         mod->sml_values =
879                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
880                         ber_dupbv( &mod->sml_values[0], &tmpval );
881                         mod->sml_values[1].bv_len = 0;
882                         mod->sml_values[1].bv_val = NULL;
883                         assert( mod->sml_values[0].bv_val );
884                         mod->sml_nvalues =
885                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
886                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
887                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
888                                         mod->sml_desc->ad_type->sat_syntax,
889                                         mod->sml_desc->ad_type->sat_equality,
890                                         mod->sml_values, mod->sml_nvalues, NULL );
891                         mod->sml_nvalues[1].bv_len = 0;
892                         mod->sml_nvalues[1].bv_val = NULL;
893                         *modtail = mod;
894                         modtail = &mod->sml_next;
895
896                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
897                         mod->sml_op = mop;
898                         mod->sml_type.bv_val = NULL;
899                         mod->sml_desc = slap_schema.si_ad_creatorsName;
900                         mod->sml_values =
901                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
902                         ber_dupbv( &mod->sml_values[0], &name );
903                         mod->sml_values[1].bv_len = 0;
904                         mod->sml_values[1].bv_val = NULL;
905                         assert( mod->sml_values[0].bv_val );
906                         mod->sml_nvalues =
907                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
908                         ber_dupbv( &mod->sml_nvalues[0], &nname );
909                         mod->sml_nvalues[1].bv_len = 0;
910                         mod->sml_nvalues[1].bv_val = NULL;
911                         assert( mod->sml_nvalues[0].bv_val );
912                         *modtail = mod;
913                         modtail = &mod->sml_next;
914
915                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
916                         mod->sml_op = mop;
917                         mod->sml_type.bv_val = NULL;
918                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
919                         mod->sml_values =
920                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
921                         ber_dupbv( &mod->sml_values[0], &timestamp );
922                         mod->sml_values[1].bv_len = 0;
923                         mod->sml_values[1].bv_val = NULL;
924                         assert( mod->sml_values[0].bv_val );
925                         mod->sml_nvalues = NULL;
926                         *modtail = mod;
927                         modtail = &mod->sml_next;
928                 }
929         }
930
931         if ( SLAP_LASTMOD( op->o_bd )) {
932                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
933                 mod->sml_op = mop;
934                 mod->sml_type.bv_val = NULL;
935                 mod->sml_desc = slap_schema.si_ad_entryCSN;
936                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
937                 ber_dupbv( &mod->sml_values[0], &csn );
938                 mod->sml_values[1].bv_len = 0;
939                 mod->sml_values[1].bv_val = NULL;
940                 assert( mod->sml_values[0].bv_val );
941                 mod->sml_nvalues = NULL;
942                 *modtail = mod;
943                 modtail = &mod->sml_next;
944
945                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
946                 mod->sml_op = mop;
947                 mod->sml_type.bv_val = NULL;
948                 mod->sml_desc = slap_schema.si_ad_modifiersName;
949                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
950                 ber_dupbv( &mod->sml_values[0], &name );
951                 mod->sml_values[1].bv_len = 0;
952                 mod->sml_values[1].bv_val = NULL;
953                 assert( mod->sml_values[0].bv_val );
954                 mod->sml_nvalues =
955                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
956                 ber_dupbv( &mod->sml_nvalues[0], &nname );
957                 mod->sml_nvalues[1].bv_len = 0;
958                 mod->sml_nvalues[1].bv_val = NULL;
959                 assert( mod->sml_nvalues[0].bv_val );
960                 *modtail = mod;
961                 modtail = &mod->sml_next;
962
963                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
964                 mod->sml_op = mop;
965                 mod->sml_type.bv_val = NULL;
966                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
967                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
968                 ber_dupbv( &mod->sml_values[0], &timestamp );
969                 mod->sml_values[1].bv_len = 0;
970                 mod->sml_values[1].bv_val = NULL;
971                 assert( mod->sml_values[0].bv_val );
972                 mod->sml_nvalues = NULL;
973                 *modtail = mod;
974                 modtail = &mod->sml_next;
975         }
976
977         *modtail = NULL;
978         return LDAP_SUCCESS;
979 }
980