]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Sync with HEAD
[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         
229         if( op->o_req_ndn.bv_len == 0 ) {
230                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
231
232                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
233                         "modify upon the root DSE not supported" );
234                 goto cleanup;
235
236         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
237                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
238
239                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
240                         "modification of subschema subentry not supported" );
241                 goto cleanup;
242         }
243
244 #ifdef LDAP_DEBUG
245         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
246
247         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
248                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
249                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
250                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
251                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
252                                         "replace")), tmp->sml_type.bv_val, 0 );
253
254                 if ( tmp->sml_values == NULL ) {
255                         Debug( LDAP_DEBUG_ARGS, "%s\n",
256                            "\t\tno values", NULL, NULL );
257                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
258                         Debug( LDAP_DEBUG_ARGS, "%s\n",
259                            "\t\tzero values", NULL, NULL );
260                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
261                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
262                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
263                 } else {
264                         Debug( LDAP_DEBUG_ARGS, "%s\n",
265                            "\t\tmultiple values", NULL, NULL );
266                 }
267         }
268
269         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
270                 char abuf[BUFSIZ/2], *ptr = abuf;
271                 int len = 0;
272
273                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
274                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
275
276                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
277                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
278                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
279                                     op->o_log_prefix, abuf, 0, 0, 0 );
280
281                                 len = 0;
282                                 ptr = abuf;
283
284                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
285                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
286                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
287                                         continue;
288                                 }
289                         }
290                         if (len) {
291                                 *ptr++ = ' ';
292                                 len++;
293                         }
294                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
295                         len += tmp->sml_type.bv_len;
296                 }
297                 if (len) {
298                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
299                                 op->o_log_prefix, abuf, 0, 0, 0 );
300                 }
301         }
302 #endif  /* LDAP_DEBUG */
303
304         manageDSAit = get_manageDSAit( op );
305
306         /*
307          * We could be serving multiple database backends.  Select the
308          * appropriate one, or send a referral to our "referral server"
309          * if we don't hold it.
310          */
311         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
312         if ( op->o_bd == NULL ) {
313                 rs->sr_ref = referral_rewrite( default_referral,
314                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
315                 if (!rs->sr_ref) rs->sr_ref = default_referral;
316
317                 if (rs->sr_ref != NULL ) {
318                         rs->sr_err = LDAP_REFERRAL;
319                         op->o_bd = frontendDB;
320                         send_ldap_result( op, rs );
321                         op->o_bd = NULL;
322
323                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
324                 } else {
325                         op->o_bd = frontendDB;
326                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
327                                 "no global superior knowledge" );
328                         op->o_bd = NULL;
329                 }
330                 goto cleanup;
331         }
332
333         /* check restrictions */
334         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
335                 send_ldap_result( op, rs );
336                 goto cleanup;
337         }
338
339         /* check for referrals */
340         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
341                 goto cleanup;
342         }
343
344         /* check for modify/increment support */
345         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
346                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
347                         "modify/increment not supported in context" );
348         }
349
350 #if defined( LDAP_SLAPI )
351 #define pb      op->o_pb
352         if ( pb ) {
353                 slapi_int_pblock_set_operation( pb, op );
354                 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)op->o_req_dn.bv_val );
355                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
356                 modv = slapi_int_modifications2ldapmods( &modlist );
357                 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
358
359                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
360                         SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
361
362                 /*
363                  * It's possible that the preoperation plugin changed the
364                  * modification array, so we need to convert it back to
365                  * a Modification list.
366                  *
367                  * Calling slapi_int_modifications2ldapmods() destroyed modlist so
368                  * we don't need to free it.
369                  */
370                 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
371                 modlist = slapi_int_ldapmods2modifications( modv );
372
373                 if ( rs->sr_err < 0 ) {
374                         /*
375                          * A preoperation plugin failure will abort the
376                          * entire operation.
377                          */
378                         Debug(LDAP_DEBUG_TRACE,
379                                 "do_modify: modify preoperation plugin failed.\n",
380                                 0, 0, 0);
381                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
382                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
383                         {
384                                 rs->sr_err = LDAP_OTHER;
385                         }
386                         slapi_int_free_ldapmods( modv );
387                         modv = NULL;
388                         goto cleanup;
389                 }
390         }
391
392         /*
393          * NB: it is valid for the plugin to return no modifications
394          * (for example, a plugin might store some attributes elsewhere
395          * and remove them from the modification list; if only those
396          * attribute types were included in the modification request,
397          * then slapi_int_ldapmods2modifications() above will return
398          * NULL).
399          *
400          * However, the post-operation plugin should still be 
401          * called.
402          */
403 #endif /* defined( LDAP_SLAPI ) */
404
405         /*
406          * do the modify if 1 && (2 || 3)
407          * 1) there is a modify function implemented in this backend;
408          * 2) this backend is master for what it holds;
409          * 3) it's a replica and the dn supplied is the update_ndn.
410          */
411         if ( op->o_bd->be_modify ) {
412                 /* do the update here */
413                 int repl_user = be_isupdate( op );
414
415                 /* Multimaster slapd does not have to check for replicator dn
416                  * because it accepts each modify request
417                  */
418 #ifndef SLAPD_MULTIMASTER
419                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
420 #endif
421                 {
422                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
423                         char            textbuf[ SLAP_TEXT_BUFLEN ];
424                         size_t          textlen = sizeof( textbuf );
425                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
426
427
428                         if ( !update ) {
429                                 rs->sr_err = slap_mods_no_update_check( modlist,
430                                                 &rs->sr_text, textbuf, textlen );
431                                 if ( rs->sr_err != LDAP_SUCCESS ) {
432                                         send_ldap_result( op, rs );
433                                         goto cleanup;
434                                 }
435                         }
436
437
438
439                         if ( !repl_user ) {
440                                 for( modtail = &modlist;
441                                         *modtail != NULL;
442                                         modtail = &(*modtail)->sml_next )
443                                 {
444                                         /* empty */
445                                 }
446
447                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
448                                         &rs->sr_text, textbuf, textlen, 1 );
449                                 if( rs->sr_err != LDAP_SUCCESS ) {
450                                         send_ldap_result( op, rs );
451                                         goto cleanup;
452                                 }
453                         }
454
455                         op->orm_modlist = modlist;
456 #ifdef SLAPD_MULTIMASTER
457                         if ( !repl_user )
458 #endif
459                         {
460                                 /* but multimaster slapd logs only the ones 
461                                  * not from a replicator user */
462                                 cb.sc_next = op->o_callback;
463                                 op->o_callback = &cb;
464                         }
465                         op->o_bd->be_modify( op, rs );
466
467 #ifndef SLAPD_MULTIMASTER
468                 /* send a referral */
469                 } else {
470                         BerVarray defref = op->o_bd->be_update_refs
471                                 ? op->o_bd->be_update_refs : default_referral;
472                         if ( defref != NULL ) {
473                                 rs->sr_ref = referral_rewrite( defref,
474                                         NULL, &op->o_req_dn,
475                                         LDAP_SCOPE_DEFAULT );
476                                 if ( rs->sr_ref == NULL ) {
477                                         /* FIXME: must duplicate, because
478                                          * overlays may muck with it */
479                                         rs->sr_ref = defref;
480                                 }
481                                 rs->sr_err = LDAP_REFERRAL;
482                                 send_ldap_result( op, rs );
483                                 if ( rs->sr_ref != defref ) {
484                                         ber_bvarray_free( rs->sr_ref );
485                                 }
486
487                         } else {
488                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
489                                         "shadow context; no update referral" );
490                         }
491 #endif
492                 }
493         } else {
494                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
495                     "operation not supported within namingContext" );
496         }
497
498 #if defined( LDAP_SLAPI )
499         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
500                 SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 )
501         {
502                 Debug(LDAP_DEBUG_TRACE,
503                         "do_modify: modify postoperation plugins failed.\n", 0, 0, 0);
504         }
505 #endif /* defined( LDAP_SLAPI ) */
506
507 cleanup:;
508 #if defined( LDAP_SLAPI )
509         if ( modv != NULL ) slapi_int_free_ldapmods( modv );
510 #endif
511
512         return rs->sr_err;
513 }
514
515 /*
516  * Do non-update constraint checking.
517  */
518 int
519 slap_mods_no_update_check(
520         Modifications *ml,
521         const char **text,
522         char *textbuf,
523         size_t textlen )
524 {
525         for ( ; ml != NULL; ml = ml->sml_next ) {
526                 if ( is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
527                         /* user modification disallowed */
528                         snprintf( textbuf, textlen,
529                                 "%s: no user modification allowed",
530                                 ml->sml_type.bv_val );
531                         *text = textbuf;
532                         return LDAP_CONSTRAINT_VIOLATION;
533                 }
534         }
535
536         return LDAP_SUCCESS;
537 }
538
539 /*
540  * Do basic attribute type checking and syntax validation.
541  */
542 int slap_mods_check(
543         Modifications *ml,
544         const char **text,
545         char *textbuf,
546         size_t textlen,
547         void *ctx )
548 {
549         int rc;
550
551         for( ; ml != NULL; ml = ml->sml_next ) {
552                 AttributeDescription *ad = NULL;
553
554                 /* convert to attribute description */
555                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
556
557                 if( rc != LDAP_SUCCESS ) {
558                         snprintf( textbuf, textlen, "%s: %s",
559                                 ml->sml_type.bv_val, *text );
560                         *text = textbuf;
561                         return rc;
562                 }
563
564                 ad = ml->sml_desc;
565
566                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
567                         && !slap_ad_is_binary( ad ))
568                 {
569                         /* attribute requires binary transfer */
570                         snprintf( textbuf, textlen,
571                                 "%s: requires ;binary transfer",
572                                 ml->sml_type.bv_val );
573                         *text = textbuf;
574                         return LDAP_UNDEFINED_TYPE;
575                 }
576
577                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
578                         && slap_ad_is_binary( ad ))
579                 {
580                         /* attribute does not require binary transfer */
581                         snprintf( textbuf, textlen,
582                                 "%s: disallows ;binary transfer",
583                                 ml->sml_type.bv_val );
584                         *text = textbuf;
585                         return LDAP_UNDEFINED_TYPE;
586                 }
587
588                 if( slap_ad_is_tag_range( ad )) {
589                         /* attribute requires binary transfer */
590                         snprintf( textbuf, textlen,
591                                 "%s: inappropriate use of tag range option",
592                                 ml->sml_type.bv_val );
593                         *text = textbuf;
594                         return LDAP_UNDEFINED_TYPE;
595                 }
596
597 #if 0
598                 /* moved to slap_mods_no_update_check() */
599                 if (!update && is_at_no_user_mod( ad->ad_type )) {
600                         /* user modification disallowed */
601                         snprintf( textbuf, textlen,
602                                 "%s: no user modification allowed",
603                                 ml->sml_type.bv_val );
604                         *text = textbuf;
605                         return LDAP_CONSTRAINT_VIOLATION;
606                 }
607 #endif
608
609                 if ( is_at_obsolete( ad->ad_type ) &&
610                         (( ml->sml_op != LDAP_MOD_REPLACE &&
611                                 ml->sml_op != LDAP_MOD_DELETE ) ||
612                                         ml->sml_values != NULL ))
613                 {
614                         /*
615                          * attribute is obsolete,
616                          * only allow replace/delete with no values
617                          */
618                         snprintf( textbuf, textlen,
619                                 "%s: attribute is obsolete",
620                                 ml->sml_type.bv_val );
621                         *text = textbuf;
622                         return LDAP_CONSTRAINT_VIOLATION;
623                 }
624
625                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
626 #ifdef SLAPD_REAL_SYNTAX
627                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
628 #endif
629                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
630                 {
631                         /*
632                          * attribute values must be INTEGER or REAL
633                          */
634                         snprintf( textbuf, textlen,
635                                 "%s: attribute syntax inappropriate for increment",
636                                 ml->sml_type.bv_val );
637                         *text = textbuf;
638                         return LDAP_CONSTRAINT_VIOLATION;
639                 }
640
641                 /*
642                  * check values
643                  */
644                 if( ml->sml_values != NULL ) {
645                         ber_len_t nvals;
646                         slap_syntax_validate_func *validate =
647                                 ad->ad_type->sat_syntax->ssyn_validate;
648                         slap_syntax_transform_func *pretty =
649                                 ad->ad_type->sat_syntax->ssyn_pretty;
650  
651                         if( !pretty && !validate ) {
652                                 *text = "no validator for syntax";
653                                 snprintf( textbuf, textlen,
654                                         "%s: no validator for syntax %s",
655                                         ml->sml_type.bv_val,
656                                         ad->ad_type->sat_syntax->ssyn_oid );
657                                 *text = textbuf;
658                                 return LDAP_INVALID_SYNTAX;
659                         }
660
661                         /*
662                          * check that each value is valid per syntax
663                          *      and pretty if appropriate
664                          */
665                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
666                                 struct berval pval;
667                                 if( pretty ) {
668                                         rc = pretty( ad->ad_type->sat_syntax,
669                                                 &ml->sml_values[nvals], &pval, ctx );
670                                 } else {
671                                         rc = validate( ad->ad_type->sat_syntax,
672                                                 &ml->sml_values[nvals] );
673                                 }
674
675                                 if( rc != 0 ) {
676                                         snprintf( textbuf, textlen,
677                                                 "%s: value #%ld invalid per syntax",
678                                                 ml->sml_type.bv_val, (long) nvals );
679                                         *text = textbuf;
680                                         return LDAP_INVALID_SYNTAX;
681                                 }
682
683                                 if( pretty ) {
684                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
685                                         ml->sml_values[nvals] = pval;
686                                 }
687                         }
688
689                         /*
690                          * a rough single value check... an additional check is needed
691                          * to catch add of single value to existing single valued attribute
692                          */
693                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
694                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
695                         {
696                                 snprintf( textbuf, textlen,
697                                         "%s: multiple values provided",
698                                         ml->sml_type.bv_val );
699                                 *text = textbuf;
700                                 return LDAP_CONSTRAINT_VIOLATION;
701                         }
702
703                         /* if the type has a normalizer, generate the
704                          * normalized values. otherwise leave them NULL.
705                          *
706                          * this is different from the rule for attributes
707                          * in an entry - in an attribute list, the normalized
708                          * value is set equal to the non-normalized value
709                          * when there is no normalizer.
710                          */
711                         if( nvals && ad->ad_type->sat_equality &&
712                                 ad->ad_type->sat_equality->smr_normalize )
713                         {
714                                 ml->sml_nvalues = ber_memalloc_x(
715                                         (nvals+1)*sizeof(struct berval), ctx );
716
717                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
718                                         rc = ad->ad_type->sat_equality->smr_normalize(
719                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
720                                                 ad->ad_type->sat_syntax,
721                                                 ad->ad_type->sat_equality,
722                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
723                                         if( rc ) {
724                                                 Debug( LDAP_DEBUG_ANY,
725                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
726                                                         rc, 0, 0 );
727                                                 snprintf( textbuf, textlen,
728                                                         "%s: value #%ld normalization failed",
729                                                         ml->sml_type.bv_val, (long) nvals );
730                                                 *text = textbuf;
731                                                 return rc;
732                                         }
733                                 }
734
735                                 ml->sml_nvalues[nvals].bv_val = NULL;
736                                 ml->sml_nvalues[nvals].bv_len = 0;
737                         }
738
739                         if( nvals ) {
740                                 /* check for duplicates */
741                                 int             i, j;
742                                 MatchingRule *mr = ad->ad_type->sat_equality;
743
744                                 /* check if the values we're adding already exist */
745                                 if( mr == NULL || !mr->smr_match ) {
746                                         for ( i = 1; ml->sml_values[i].bv_val != NULL; i++ ) {
747                                                 /* test asserted values against themselves */
748                                                 for( j = 0; j < i; j++ ) {
749                                                         if ( bvmatch( &ml->sml_values[i],
750                                                                 &ml->sml_values[j] ) )
751                                                         {
752                                                                 /* value exists already */
753                                                                 snprintf( textbuf, textlen,
754                                                                         "%s: value #%d provided more than once",
755                                                                         ml->sml_desc->ad_cname.bv_val, j );
756                                                                 *text = textbuf;
757                                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
758                                                         }
759                                                 }
760                                         }
761
762                                 } else {
763                                         int rc;
764                                         int match;
765
766                                         for ( i = 1; ml->sml_values[i].bv_val != NULL; i++ ) {
767                                                 /* test asserted values against themselves */
768                                                 for( j = 0; j < i; j++ ) {
769                                                         rc = value_match( &match, ml->sml_desc, mr,
770                                                                 SLAP_MR_EQUALITY
771                                                                         | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
772                                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
773                                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
774                                                                 ml->sml_nvalues
775                                                                         ? &ml->sml_nvalues[i]
776                                                                         : &ml->sml_values[i],
777                                                                 ml->sml_nvalues
778                                                                         ? &ml->sml_nvalues[j]
779                                                                         : &ml->sml_values[j],
780                                                                 text );
781                                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
782                                                                 /* value exists already */
783                                                                 snprintf( textbuf, textlen,
784                                                                         "%s: value #%d provided more than once",
785                                                                         ml->sml_desc->ad_cname.bv_val, j );
786                                                                 *text = textbuf;
787                                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
788
789                                                         } else if ( rc != LDAP_SUCCESS ) {
790                                                                 return rc;
791                                                         }
792                                                 }
793                                         }
794                                 }
795                         }
796
797                 }
798         }
799
800         return LDAP_SUCCESS;
801 }
802
803 int slap_mods_opattrs(
804         Operation *op,
805         Modifications *mods,
806         Modifications **modtail,
807         const char **text,
808         char *textbuf, size_t textlen,
809         int manage_ctxcsn )
810 {
811         struct berval name, timestamp, csn;
812         struct berval nname;
813         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
814         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
815         Modifications *mod;
816
817         int mop = op->o_tag == LDAP_REQ_ADD
818                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
819
820         assert( modtail != NULL );
821         assert( *modtail == NULL );
822
823         if ( SLAP_LASTMOD( op->o_bd )) {
824                 struct tm *ltm;
825 #ifdef HAVE_GMTIME_R
826                 struct tm ltm_buf;
827 #endif
828                 time_t now = slap_get_time();
829
830 #ifdef HAVE_GMTIME_R
831                 ltm = gmtime_r( &now, &ltm_buf );
832 #else
833                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
834                 ltm = gmtime( &now );
835 #endif /* HAVE_GMTIME_R */
836                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
837
838                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, manage_ctxcsn );
839
840 #ifndef HAVE_GMTIME_R
841                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
842 #endif
843
844                 timestamp.bv_val = timebuf;
845                 timestamp.bv_len = strlen(timebuf);
846
847                 if( op->o_dn.bv_len == 0 ) {
848                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
849                         nname = name;
850                 } else {
851                         name = op->o_dn;
852                         nname = op->o_ndn;
853                 }
854         }
855
856         if( op->o_tag == LDAP_REQ_ADD ) {
857                 struct berval tmpval;
858
859                 if( global_schemacheck ) {
860                         int rc = mods_structural_class( mods, &tmpval,
861                                 text, textbuf, textlen );
862                         if( rc != LDAP_SUCCESS ) return rc;
863
864                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
865                         mod->sml_op = mop;
866                         mod->sml_type.bv_val = NULL;
867                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
868                         mod->sml_values =
869                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
870                         ber_dupbv( &mod->sml_values[0], &tmpval );
871                         mod->sml_values[1].bv_len = 0;
872                         mod->sml_values[1].bv_val = NULL;
873                         assert( mod->sml_values[0].bv_val );
874                         mod->sml_nvalues =
875                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
876                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
877                         mod->sml_nvalues[1].bv_len = 0;
878                         mod->sml_nvalues[1].bv_val = NULL;
879                         assert( mod->sml_nvalues[0].bv_val );
880                         *modtail = mod;
881                         modtail = &mod->sml_next;
882                 }
883
884                 if ( SLAP_LASTMOD( op->o_bd )) {
885                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
886
887                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
888                         tmpval.bv_val = uuidbuf;
889                 
890                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
891                         mod->sml_op = mop;
892                         mod->sml_type.bv_val = NULL;
893                         mod->sml_desc = slap_schema.si_ad_entryUUID;
894                         mod->sml_values =
895                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
896                         ber_dupbv( &mod->sml_values[0], &tmpval );
897                         mod->sml_values[1].bv_len = 0;
898                         mod->sml_values[1].bv_val = NULL;
899                         assert( mod->sml_values[0].bv_val );
900                         mod->sml_nvalues =
901                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
902                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
903                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
904                                         mod->sml_desc->ad_type->sat_syntax,
905                                         mod->sml_desc->ad_type->sat_equality,
906                                         mod->sml_values, mod->sml_nvalues, NULL );
907                         mod->sml_nvalues[1].bv_len = 0;
908                         mod->sml_nvalues[1].bv_val = NULL;
909                         *modtail = mod;
910                         modtail = &mod->sml_next;
911
912                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
913                         mod->sml_op = mop;
914                         mod->sml_type.bv_val = NULL;
915                         mod->sml_desc = slap_schema.si_ad_creatorsName;
916                         mod->sml_values =
917                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
918                         ber_dupbv( &mod->sml_values[0], &name );
919                         mod->sml_values[1].bv_len = 0;
920                         mod->sml_values[1].bv_val = NULL;
921                         assert( mod->sml_values[0].bv_val );
922                         mod->sml_nvalues =
923                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
924                         ber_dupbv( &mod->sml_nvalues[0], &nname );
925                         mod->sml_nvalues[1].bv_len = 0;
926                         mod->sml_nvalues[1].bv_val = NULL;
927                         assert( mod->sml_nvalues[0].bv_val );
928                         *modtail = mod;
929                         modtail = &mod->sml_next;
930
931                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
932                         mod->sml_op = mop;
933                         mod->sml_type.bv_val = NULL;
934                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
935                         mod->sml_values =
936                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
937                         ber_dupbv( &mod->sml_values[0], &timestamp );
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         }
946
947         if ( SLAP_LASTMOD( op->o_bd )) {
948                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
949                 mod->sml_op = mop;
950                 mod->sml_type.bv_val = NULL;
951                 mod->sml_desc = slap_schema.si_ad_entryCSN;
952                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
953                 ber_dupbv( &mod->sml_values[0], &csn );
954                 mod->sml_values[1].bv_len = 0;
955                 mod->sml_values[1].bv_val = NULL;
956                 assert( mod->sml_values[0].bv_val );
957                 mod->sml_nvalues = NULL;
958                 *modtail = mod;
959                 modtail = &mod->sml_next;
960
961                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
962                 mod->sml_op = mop;
963                 mod->sml_type.bv_val = NULL;
964                 mod->sml_desc = slap_schema.si_ad_modifiersName;
965                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
966                 ber_dupbv( &mod->sml_values[0], &name );
967                 mod->sml_values[1].bv_len = 0;
968                 mod->sml_values[1].bv_val = NULL;
969                 assert( mod->sml_values[0].bv_val );
970                 mod->sml_nvalues =
971                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
972                 ber_dupbv( &mod->sml_nvalues[0], &nname );
973                 mod->sml_nvalues[1].bv_len = 0;
974                 mod->sml_nvalues[1].bv_val = NULL;
975                 assert( mod->sml_nvalues[0].bv_val );
976                 *modtail = mod;
977                 modtail = &mod->sml_next;
978
979                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
980                 mod->sml_op = mop;
981                 mod->sml_type.bv_val = NULL;
982                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
983                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
984                 ber_dupbv( &mod->sml_values[0], &timestamp );
985                 mod->sml_values[1].bv_len = 0;
986                 mod->sml_values[1].bv_val = NULL;
987                 assert( mod->sml_values[0].bv_val );
988                 mod->sml_nvalues = NULL;
989                 *modtail = mod;
990                 modtail = &mod->sml_next;
991         }
992
993         *modtail = NULL;
994         return LDAP_SUCCESS;
995 }
996