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