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