]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
honor disclose
[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 ( modlist != NULL ) slap_mods_free( 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                         if( nvals ) {
748                                 /* check for duplicates */
749                                 int             i, j;
750                                 MatchingRule *mr = ad->ad_type->sat_equality;
751
752                                 /* check if the values we're adding already exist */
753                                 if( mr == NULL || !mr->smr_match ) {
754                                         for ( i = 1; ml->sml_values[i].bv_val != NULL; i++ ) {
755                                                 /* test asserted values against themselves */
756                                                 for( j = 0; j < i; j++ ) {
757                                                         if ( bvmatch( &ml->sml_values[i],
758                                                                 &ml->sml_values[j] ) )
759                                                         {
760                                                                 /* value exists already */
761                                                                 snprintf( textbuf, textlen,
762                                                                         "%s: value #%d provided more than once",
763                                                                         ml->sml_desc->ad_cname.bv_val, j );
764                                                                 *text = textbuf;
765                                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
766                                                         }
767                                                 }
768                                         }
769
770                                 } else {
771                                         int rc;
772                                         int match;
773
774                                         for ( i = 1; ml->sml_values[i].bv_val != NULL; i++ ) {
775                                                 /* test asserted values against themselves */
776                                                 for( j = 0; j < i; j++ ) {
777                                                         rc = value_match( &match, ml->sml_desc, mr,
778                                                                 SLAP_MR_EQUALITY
779                                                                         | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
780                                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
781                                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
782                                                                 ml->sml_nvalues
783                                                                         ? &ml->sml_nvalues[i]
784                                                                         : &ml->sml_values[i],
785                                                                 ml->sml_nvalues
786                                                                         ? &ml->sml_nvalues[j]
787                                                                         : &ml->sml_values[j],
788                                                                 text );
789                                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
790                                                                 /* value exists already */
791                                                                 snprintf( textbuf, textlen,
792                                                                         "%s: value #%d provided more than once",
793                                                                         ml->sml_desc->ad_cname.bv_val, j );
794                                                                 *text = textbuf;
795                                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
796
797                                                         } else if ( rc != LDAP_SUCCESS ) {
798                                                                 return rc;
799                                                         }
800                                                 }
801                                         }
802                                 }
803                         }
804
805                 }
806         }
807
808         return LDAP_SUCCESS;
809 }
810
811 int slap_mods_opattrs(
812         Operation *op,
813         Modifications *mods,
814         Modifications **modtail,
815         const char **text,
816         char *textbuf, size_t textlen,
817         int manage_ctxcsn )
818 {
819         struct berval name, timestamp, csn;
820         struct berval nname;
821         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
822         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
823         Modifications *mod;
824
825         int mop = op->o_tag == LDAP_REQ_ADD
826                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
827
828         assert( modtail != NULL );
829         assert( *modtail == NULL );
830
831         if ( SLAP_LASTMOD( op->o_bd )) {
832                 struct tm *ltm;
833 #ifdef HAVE_GMTIME_R
834                 struct tm ltm_buf;
835 #endif
836                 time_t now = slap_get_time();
837
838 #ifdef HAVE_GMTIME_R
839                 ltm = gmtime_r( &now, &ltm_buf );
840 #else
841                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
842                 ltm = gmtime( &now );
843 #endif /* HAVE_GMTIME_R */
844                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
845
846                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, manage_ctxcsn );
847
848 #ifndef HAVE_GMTIME_R
849                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
850 #endif
851
852                 timestamp.bv_val = timebuf;
853                 timestamp.bv_len = strlen(timebuf);
854
855                 if( op->o_dn.bv_len == 0 ) {
856                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
857                         nname = name;
858                 } else {
859                         name = op->o_dn;
860                         nname = op->o_ndn;
861                 }
862         }
863
864         if( op->o_tag == LDAP_REQ_ADD ) {
865                 struct berval tmpval;
866
867                 if( global_schemacheck ) {
868                         int rc = mods_structural_class( mods, &tmpval,
869                                 text, textbuf, textlen );
870                         if( rc != LDAP_SUCCESS ) return rc;
871
872                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
873                         mod->sml_op = mop;
874                         mod->sml_type.bv_val = NULL;
875                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
876                         mod->sml_values =
877                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
878                         ber_dupbv( &mod->sml_values[0], &tmpval );
879                         mod->sml_values[1].bv_len = 0;
880                         mod->sml_values[1].bv_val = NULL;
881                         assert( mod->sml_values[0].bv_val );
882                         mod->sml_nvalues =
883                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
884                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
885                         mod->sml_nvalues[1].bv_len = 0;
886                         mod->sml_nvalues[1].bv_val = NULL;
887                         assert( mod->sml_nvalues[0].bv_val );
888                         *modtail = mod;
889                         modtail = &mod->sml_next;
890                 }
891
892                 if ( SLAP_LASTMOD( op->o_bd )) {
893                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
894
895                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
896                         tmpval.bv_val = uuidbuf;
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_entryUUID;
902                         mod->sml_values =
903                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
904                         ber_dupbv( &mod->sml_values[0], &tmpval );
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                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
911                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
912                                         mod->sml_desc->ad_type->sat_syntax,
913                                         mod->sml_desc->ad_type->sat_equality,
914                                         mod->sml_values, mod->sml_nvalues, NULL );
915                         mod->sml_nvalues[1].bv_len = 0;
916                         mod->sml_nvalues[1].bv_val = NULL;
917                         *modtail = mod;
918                         modtail = &mod->sml_next;
919
920                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
921                         mod->sml_op = mop;
922                         mod->sml_type.bv_val = NULL;
923                         mod->sml_desc = slap_schema.si_ad_creatorsName;
924                         mod->sml_values =
925                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
926                         ber_dupbv( &mod->sml_values[0], &name );
927                         mod->sml_values[1].bv_len = 0;
928                         mod->sml_values[1].bv_val = NULL;
929                         assert( mod->sml_values[0].bv_val );
930                         mod->sml_nvalues =
931                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
932                         ber_dupbv( &mod->sml_nvalues[0], &nname );
933                         mod->sml_nvalues[1].bv_len = 0;
934                         mod->sml_nvalues[1].bv_val = NULL;
935                         assert( mod->sml_nvalues[0].bv_val );
936                         *modtail = mod;
937                         modtail = &mod->sml_next;
938
939                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
940                         mod->sml_op = mop;
941                         mod->sml_type.bv_val = NULL;
942                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
943                         mod->sml_values =
944                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
945                         ber_dupbv( &mod->sml_values[0], &timestamp );
946                         mod->sml_values[1].bv_len = 0;
947                         mod->sml_values[1].bv_val = NULL;
948                         assert( mod->sml_values[0].bv_val );
949                         mod->sml_nvalues = NULL;
950                         *modtail = mod;
951                         modtail = &mod->sml_next;
952                 }
953         }
954
955         if ( SLAP_LASTMOD( op->o_bd )) {
956                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
957                 mod->sml_op = mop;
958                 mod->sml_type.bv_val = NULL;
959                 mod->sml_desc = slap_schema.si_ad_entryCSN;
960                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
961                 ber_dupbv( &mod->sml_values[0], &csn );
962                 mod->sml_values[1].bv_len = 0;
963                 mod->sml_values[1].bv_val = NULL;
964                 assert( mod->sml_values[0].bv_val );
965                 mod->sml_nvalues = NULL;
966                 *modtail = mod;
967                 modtail = &mod->sml_next;
968
969                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
970                 mod->sml_op = mop;
971                 mod->sml_type.bv_val = NULL;
972                 mod->sml_desc = slap_schema.si_ad_modifiersName;
973                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
974                 ber_dupbv( &mod->sml_values[0], &name );
975                 mod->sml_values[1].bv_len = 0;
976                 mod->sml_values[1].bv_val = NULL;
977                 assert( mod->sml_values[0].bv_val );
978                 mod->sml_nvalues =
979                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
980                 ber_dupbv( &mod->sml_nvalues[0], &nname );
981                 mod->sml_nvalues[1].bv_len = 0;
982                 mod->sml_nvalues[1].bv_val = NULL;
983                 assert( mod->sml_nvalues[0].bv_val );
984                 *modtail = mod;
985                 modtail = &mod->sml_next;
986
987                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
988                 mod->sml_op = mop;
989                 mod->sml_type.bv_val = NULL;
990                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
991                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
992                 ber_dupbv( &mod->sml_values[0], &timestamp );
993                 mod->sml_values[1].bv_len = 0;
994                 mod->sml_values[1].bv_val = NULL;
995                 assert( mod->sml_values[0].bv_val );
996                 mod->sml_nvalues = NULL;
997                 *modtail = mod;
998                 modtail = &mod->sml_next;
999         }
1000
1001         *modtail = NULL;
1002         return LDAP_SUCCESS;
1003 }
1004