]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
90c7d3c3a0901f60388365b006b26d5ae7511f27
[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, "{e{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 ( !BER_BVISNULL( &mod->sml_values[ 1 ] ) ) {
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,
188                 &rs->sr_text, 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         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
204         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
205         if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
206
207         return rs->sr_err;
208 }
209
210 int
211 fe_op_modify( Operation *op, SlapReply *rs )
212 {
213 #ifdef LDAP_DEBUG
214         Modifications   *tmp;
215 #endif
216         int             manageDSAit;
217         Modifications   *modlist = op->orm_modlist;
218         int             increment = op->orm_increment;
219         BackendDB *op_be;
220         char            textbuf[ SLAP_TEXT_BUFLEN ];
221         size_t          textlen = sizeof( textbuf );
222         
223         if( BER_BVISEMPTY( &op->o_req_ndn ) ) {
224                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
225
226                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
227                         "modify upon the root DSE not supported" );
228                 goto cleanup;
229
230         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
231                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
232
233                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
234                         "modification of subschema subentry not supported" );
235                 goto cleanup;
236         }
237
238 #ifdef LDAP_DEBUG
239         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
240
241         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
242                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
243                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
244                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
245                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
246                                         "replace")), tmp->sml_type.bv_val, 0 );
247
248                 if ( tmp->sml_values == NULL ) {
249                         Debug( LDAP_DEBUG_ARGS, "%s\n",
250                            "\t\tno values", NULL, NULL );
251                 } else if ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
252                         Debug( LDAP_DEBUG_ARGS, "%s\n",
253                            "\t\tzero values", NULL, NULL );
254                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
255                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
256                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
257                 } else {
258                         Debug( LDAP_DEBUG_ARGS, "%s\n",
259                            "\t\tmultiple values", NULL, NULL );
260                 }
261         }
262
263         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
264                 char abuf[BUFSIZ/2], *ptr = abuf;
265                 int len = 0;
266
267                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
268                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
269
270                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
271                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
272                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
273                                     op->o_log_prefix, abuf, 0, 0, 0 );
274
275                                 len = 0;
276                                 ptr = abuf;
277
278                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
279                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
280                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
281                                         continue;
282                                 }
283                         }
284                         if (len) {
285                                 *ptr++ = ' ';
286                                 len++;
287                         }
288                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
289                         len += tmp->sml_type.bv_len;
290                 }
291                 if (len) {
292                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
293                                 op->o_log_prefix, abuf, 0, 0, 0 );
294                 }
295         }
296 #endif  /* LDAP_DEBUG */
297
298         manageDSAit = get_manageDSAit( op );
299
300         /*
301          * We could be serving multiple database backends.  Select the
302          * appropriate one, or send a referral to our "referral server"
303          * if we don't hold it.
304          */
305         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
306         if ( op->o_bd == NULL ) {
307                 rs->sr_ref = referral_rewrite( default_referral,
308                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
309                 if (!rs->sr_ref) rs->sr_ref = default_referral;
310
311                 if (rs->sr_ref != NULL ) {
312                         rs->sr_err = LDAP_REFERRAL;
313                         op->o_bd = frontendDB;
314                         send_ldap_result( op, rs );
315                         op->o_bd = NULL;
316
317                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
318                 } else {
319                         op->o_bd = frontendDB;
320                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
321                                 "no global superior knowledge" );
322                         op->o_bd = NULL;
323                 }
324                 goto cleanup;
325         }
326
327         /* If we've got a glued backend, check the real backend */
328         op_be = op->o_bd;
329         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
330                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
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         rs->sr_err = slap_mods_obsolete_check( op, modlist,
345                 &rs->sr_text, textbuf, textlen );
346         if ( rs->sr_err != LDAP_SUCCESS ) {
347                 send_ldap_result( op, rs );
348                 goto cleanup;
349         }
350
351         /* check for modify/increment support */
352         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
353                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
354                         "modify/increment not supported in context" );
355         }
356
357         /*
358          * do the modify if 1 && (2 || 3)
359          * 1) there is a modify function implemented in this backend;
360          * 2) this backend is master for what it holds;
361          * 3) it's a replica and the dn supplied is the update_ndn.
362          */
363         if ( op->o_bd->be_modify ) {
364                 /* do the update here */
365                 int repl_user = be_isupdate( op );
366
367                 /* Multimaster slapd does not have to check for replicator dn
368                  * because it accepts each modify request
369                  */
370 #ifndef SLAPD_MULTIMASTER
371                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
372 #endif
373                 {
374                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
375                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
376
377                         op->o_bd = op_be;
378
379                         if ( !update ) {
380                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
381                                         &rs->sr_text, textbuf, textlen );
382                                 if ( rs->sr_err != LDAP_SUCCESS ) {
383                                         send_ldap_result( op, rs );
384                                         goto cleanup;
385                                 }
386                         }
387
388                         op->orm_modlist = modlist;
389 #ifdef SLAPD_MULTIMASTER
390                         if ( !repl_user )
391 #endif
392                         {
393                                 /* but multimaster slapd logs only the ones 
394                                  * not from a replicator user */
395                                 cb.sc_next = op->o_callback;
396                                 op->o_callback = &cb;
397                         }
398                         op->o_bd->be_modify( op, rs );
399
400 #ifndef SLAPD_MULTIMASTER
401                 /* send a referral */
402                 } else {
403                         BerVarray defref = op->o_bd->be_update_refs
404                                 ? op->o_bd->be_update_refs : default_referral;
405                         if ( defref != NULL ) {
406                                 rs->sr_ref = referral_rewrite( defref,
407                                         NULL, &op->o_req_dn,
408                                         LDAP_SCOPE_DEFAULT );
409                                 if ( rs->sr_ref == NULL ) {
410                                         /* FIXME: must duplicate, because
411                                          * overlays may muck with it */
412                                         rs->sr_ref = defref;
413                                 }
414                                 rs->sr_err = LDAP_REFERRAL;
415                                 send_ldap_result( op, rs );
416                                 if ( rs->sr_ref != defref ) {
417                                         ber_bvarray_free( rs->sr_ref );
418                                 }
419
420                         } else {
421                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
422                                         "shadow context; no update referral" );
423                         }
424 #endif
425                 }
426         } else {
427                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
428                     "operation not supported within namingContext" );
429         }
430
431 cleanup:;
432         return rs->sr_err;
433 }
434
435 /*
436  * Obsolete constraint checking.
437  */
438 int
439 slap_mods_obsolete_check(
440         Operation *op,
441         Modifications *ml,
442         const char **text,
443         char *textbuf,
444         size_t textlen )
445 {
446         if( get_manageDIT( op ) ) return LDAP_SUCCESS;
447
448         for ( ; ml != NULL; ml = ml->sml_next ) {
449                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
450                         (( ml->sml_op != LDAP_MOD_REPLACE &&
451                                 ml->sml_op != LDAP_MOD_DELETE ) ||
452                                         ml->sml_values != NULL ))
453                 {
454                         /*
455                          * attribute is obsolete,
456                          * only allow replace/delete with no values
457                          */
458                         snprintf( textbuf, textlen,
459                                 "%s: attribute is obsolete",
460                                 ml->sml_type.bv_val );
461                         *text = textbuf;
462                         return LDAP_CONSTRAINT_VIOLATION;
463                 }
464         }
465
466         return LDAP_SUCCESS;
467 }
468
469 /*
470  * No-user-modification constraint checking.
471  */
472 int
473 slap_mods_no_user_mod_check(
474         Operation *op,
475         Modifications *ml,
476         const char **text,
477         char *textbuf,
478         size_t textlen )
479 {
480         for ( ; ml != NULL; ml = ml->sml_next ) {
481                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) continue;
482
483                 if ( get_manageDIT( op ) ) {
484                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
485                                 ml->sml_flags |= SLAP_MOD_MANAGING;
486                                 continue;
487                         }
488
489                         /* attribute not manageable */
490                         snprintf( textbuf, textlen,
491                                 "%s: no-user-modification attribute not manageable",
492                                 ml->sml_type.bv_val );
493
494                 } else {
495                         /* user modification disallowed */
496                         snprintf( textbuf, textlen,
497                                 "%s: no user modification allowed",
498                                 ml->sml_type.bv_val );
499                 }
500
501                 *text = textbuf;
502                 return LDAP_CONSTRAINT_VIOLATION;
503         }
504
505         return LDAP_SUCCESS;
506 }
507
508 int
509 slap_mods_no_repl_user_mod_check(
510         Operation *op,
511         Modifications *ml,
512         const char **text,
513         char *textbuf,
514         size_t textlen )
515 {
516         Modifications *mods;
517         Modifications *modp;
518
519         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
520                 assert( mods->sml_op == LDAP_MOD_ADD );
521
522                 /* check doesn't already appear */
523                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
524                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
525                                 snprintf( textbuf, textlen,
526                                         "attribute '%s' provided more than once",
527                                         mods->sml_desc->ad_cname.bv_val );
528                                 *text = textbuf;
529                                 return LDAP_TYPE_OR_VALUE_EXISTS;
530                         }
531                 }
532         }
533
534         return LDAP_SUCCESS;
535 }
536
537 /*
538  * Do basic attribute type checking and syntax validation.
539  */
540 int slap_mods_check(
541         Modifications *ml,
542         const char **text,
543         char *textbuf,
544         size_t textlen,
545         void *ctx )
546 {
547         int rc;
548
549         for( ; ml != NULL; ml = ml->sml_next ) {
550                 AttributeDescription *ad = NULL;
551
552                 /* convert to attribute description */
553                 if ( ml->sml_desc == NULL ) {
554                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
555                         if( rc != LDAP_SUCCESS ) {
556                                 snprintf( textbuf, textlen, "%s: %s",
557                                         ml->sml_type.bv_val, *text );
558                                 *text = textbuf;
559                                 return rc;
560                         }
561                 }
562
563                 ad = ml->sml_desc;
564
565                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
566                         && !slap_ad_is_binary( ad ))
567                 {
568                         /* attribute requires binary transfer */
569                         snprintf( textbuf, textlen,
570                                 "%s: requires ;binary transfer",
571                                 ml->sml_type.bv_val );
572                         *text = textbuf;
573                         return LDAP_UNDEFINED_TYPE;
574                 }
575
576                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
577                         && slap_ad_is_binary( ad ))
578                 {
579                         /* attribute does not require binary transfer */
580                         snprintf( textbuf, textlen,
581                                 "%s: disallows ;binary transfer",
582                                 ml->sml_type.bv_val );
583                         *text = textbuf;
584                         return LDAP_UNDEFINED_TYPE;
585                 }
586
587                 if( slap_ad_is_tag_range( ad )) {
588                         /* attribute requires binary transfer */
589                         snprintf( textbuf, textlen,
590                                 "%s: inappropriate use of tag range option",
591                                 ml->sml_type.bv_val );
592                         *text = textbuf;
593                         return LDAP_UNDEFINED_TYPE;
594                 }
595
596 #if 0
597                 if ( is_at_obsolete( ad->ad_type ) &&
598                         (( ml->sml_op != LDAP_MOD_REPLACE &&
599                                 ml->sml_op != LDAP_MOD_DELETE ) ||
600                                         ml->sml_values != NULL ))
601                 {
602                         /*
603                          * attribute is obsolete,
604                          * only allow replace/delete with no values
605                          */
606                         snprintf( textbuf, textlen,
607                                 "%s: attribute is obsolete",
608                                 ml->sml_type.bv_val );
609                         *text = textbuf;
610                         return LDAP_CONSTRAINT_VIOLATION;
611                 }
612 #endif
613
614                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
615 #ifdef SLAPD_REAL_SYNTAX
616                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
617 #endif
618                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
619                 {
620                         /*
621                          * attribute values must be INTEGER or REAL
622                          */
623                         snprintf( textbuf, textlen,
624                                 "%s: attribute syntax inappropriate for increment",
625                                 ml->sml_type.bv_val );
626                         *text = textbuf;
627                         return LDAP_CONSTRAINT_VIOLATION;
628                 }
629
630                 /*
631                  * check values
632                  */
633                 if( ml->sml_values != NULL ) {
634                         ber_len_t nvals;
635                         slap_syntax_validate_func *validate =
636                                 ad->ad_type->sat_syntax->ssyn_validate;
637                         slap_syntax_transform_func *pretty =
638                                 ad->ad_type->sat_syntax->ssyn_pretty;
639  
640                         if( !pretty && !validate ) {
641                                 *text = "no validator for syntax";
642                                 snprintf( textbuf, textlen,
643                                         "%s: no validator for syntax %s",
644                                         ml->sml_type.bv_val,
645                                         ad->ad_type->sat_syntax->ssyn_oid );
646                                 *text = textbuf;
647                                 return LDAP_INVALID_SYNTAX;
648                         }
649
650                         /*
651                          * check that each value is valid per syntax
652                          *      and pretty if appropriate
653                          */
654                         for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
655                                 struct berval pval;
656
657                                 if ( pretty ) {
658 #ifdef SLAP_ORDERED_PRETTYNORM
659                                         rc = ordered_value_pretty( ad,
660                                                 &ml->sml_values[nvals], &pval, ctx );
661 #else /* ! SLAP_ORDERED_PRETTYNORM */
662                                         rc = pretty( ad->ad_type->sat_syntax,
663                                                 &ml->sml_values[nvals], &pval, ctx );
664 #endif /* ! SLAP_ORDERED_PRETTYNORM */
665                                 } else {
666 #ifdef SLAP_ORDERED_PRETTYNORM
667                                         rc = ordered_value_validate( ad,
668                                                 &ml->sml_values[nvals] );
669 #else /* ! SLAP_ORDERED_PRETTYNORM */
670                                         rc = validate( ad->ad_type->sat_syntax,
671                                                 &ml->sml_values[nvals] );
672 #endif /* ! SLAP_ORDERED_PRETTYNORM */
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; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
718 #ifdef SLAP_ORDERED_PRETTYNORM
719                                         rc = ordered_value_normalize(
720                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
721                                                 ad,
722                                                 ad->ad_type->sat_equality,
723                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
724 #else /* ! SLAP_ORDERED_PRETTYNORM */
725                                         rc = ad->ad_type->sat_equality->smr_normalize(
726                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
727                                                 ad->ad_type->sat_syntax,
728                                                 ad->ad_type->sat_equality,
729                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
730 #endif /* ! SLAP_ORDERED_PRETTYNORM */
731                                         if ( rc ) {
732                                                 Debug( LDAP_DEBUG_ANY,
733                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
734                                                         rc, 0, 0 );
735                                                 snprintf( textbuf, textlen,
736                                                         "%s: value #%ld normalization failed",
737                                                         ml->sml_type.bv_val, (long) nvals );
738                                                 *text = textbuf;
739                                                 return rc;
740                                         }
741                                 }
742
743                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
744                         }
745
746                         /* check for duplicates, but ignore Deletes.
747                          */
748                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
749                                 int             i, j, rc, match;
750                                 MatchingRule *mr = ad->ad_type->sat_equality;
751
752                                 for ( i = 1; i < nvals ; i++ ) {
753                                         /* test asserted values against themselves */
754                                         for( j = 0; j < i; j++ ) {
755                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
756                                                         SLAP_MR_EQUALITY
757                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
758                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
759                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
760                                                         ml->sml_nvalues
761                                                                 ? &ml->sml_nvalues[i]
762                                                                 : &ml->sml_values[i],
763                                                         ml->sml_nvalues
764                                                                 ? &ml->sml_nvalues[j]
765                                                                 : &ml->sml_values[j],
766                                                         text );
767                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
768                                                         /* value exists already */
769                                                         snprintf( textbuf, textlen,
770                                                                 "%s: value #%d provided more than once",
771                                                                 ml->sml_desc->ad_cname.bv_val, j );
772                                                         *text = textbuf;
773                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
774
775                                                 } else if ( rc != LDAP_SUCCESS ) {
776                                                         return rc;
777                                                 }
778                                         }
779                                 }
780                         }
781
782                 }
783         }
784
785         return LDAP_SUCCESS;
786 }
787
788 /* Enter with bv->bv_len = sizeof buffer, returns with
789  * actual length of string
790  */
791 void slap_timestamp( time_t *tm, struct berval *bv )
792 {
793         struct tm *ltm;
794 #ifdef HAVE_GMTIME_R
795         struct tm ltm_buf;
796
797         ltm = gmtime_r( tm, &ltm_buf );
798 #else
799         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
800         ltm = gmtime( tm );
801 #endif
802
803         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
804
805 #ifndef HAVE_GMTIME_R
806         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
807 #endif
808 }
809
810 /* modify only calls this for non-replicas. modrdn always calls.
811  */
812 void slap_mods_opattrs(
813         Operation *op,
814         Modifications *mods,
815         int manage_ctxcsn )
816 {
817         struct berval name, timestamp, csn = BER_BVNULL;
818         struct berval nname;
819         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
820         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
821         Modifications *mod, **modtail, *modlast;
822
823         if ( SLAP_LASTMOD( op->o_bd ) ) {
824                 char *ptr;
825                 timestamp.bv_val = timebuf;
826                 if ( BER_BVISEMPTY( &op->o_csn )) {
827                         csn.bv_val = csnbuf;
828                         csn.bv_len = sizeof( csnbuf );
829                         slap_get_csn( op, &csn, manage_ctxcsn );
830                 } else {
831                         csn = op->o_csn;
832                 }
833                 ptr = ber_bvchr( &csn, '#' );
834                 if ( ptr && ptr < &csn.bv_val[csn.bv_len] ) {
835                         timestamp.bv_len = ptr - csn.bv_val;
836                         if ( timestamp.bv_len >= sizeof( timebuf ))
837                                 timestamp.bv_len = sizeof( timebuf ) - 1;
838                         strncpy( timebuf, csn.bv_val, timestamp.bv_len );
839                         timebuf[timestamp.bv_len] = '\0';
840                 } else {
841                         time_t now = slap_get_time();
842
843                         timestamp.bv_len = sizeof(timebuf);
844
845                         slap_timestamp( &now, &timestamp );
846                 }
847
848                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
849                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
850                         nname = name;
851                 } else {
852                         name = op->o_dn;
853                         nname = op->o_ndn;
854                 }
855
856                 for ( mod = mods; mod->sml_next; mod = mod->sml_next )
857                         ;
858                 modtail = &mod->sml_next;
859
860                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
861                 mod->sml_op = LDAP_MOD_REPLACE;
862                 mod->sml_flags = SLAP_MOD_INTERNAL;
863                 mod->sml_next = NULL;
864                 BER_BVZERO( &mod->sml_type );
865                 mod->sml_desc = slap_schema.si_ad_entryCSN;
866                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
867                 ber_dupbv( &mod->sml_values[0], &csn );
868                 BER_BVZERO( &mod->sml_values[1] );
869                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
870                 mod->sml_nvalues = NULL;
871                 *modtail = mod;
872                 modlast = mod;
873                 modtail = &mod->sml_next;
874         
875                 if ( get_manageDIT( op ) ) {
876                         for ( mod = mods; mod != modlast; mod = mod->sml_next ) {
877                                 if ( mod->sml_desc == slap_schema.si_ad_modifiersName ) {
878                                         break;
879                                 }
880                         }
881                 }
882
883                 if ( mod->sml_desc != slap_schema.si_ad_modifiersName ) {
884                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
885                         mod->sml_op = LDAP_MOD_REPLACE;
886                         mod->sml_flags = SLAP_MOD_INTERNAL;
887                         mod->sml_next = NULL;
888                         BER_BVZERO( &mod->sml_type );
889                         mod->sml_desc = slap_schema.si_ad_modifiersName;
890                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
891                         ber_dupbv( &mod->sml_values[0], &name );
892                         BER_BVZERO( &mod->sml_values[1] );
893                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
894                         mod->sml_nvalues =
895                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
896                         ber_dupbv( &mod->sml_nvalues[0], &nname );
897                         BER_BVZERO( &mod->sml_nvalues[1] );
898                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
899                         *modtail = mod;
900                         modtail = &mod->sml_next;
901                 }
902
903                 if ( get_manageDIT( op ) ) {
904                         for ( mod = mods; mod != modlast; mod = mod->sml_next ) {
905                                 if ( mod->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
906                                         break;
907                                 }
908                         }
909                 }
910
911                 if ( mod->sml_desc != slap_schema.si_ad_modifyTimestamp ) {
912                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
913                         mod->sml_op = LDAP_MOD_REPLACE;
914                         mod->sml_flags = SLAP_MOD_INTERNAL;
915                         mod->sml_next = NULL;
916                         BER_BVZERO( &mod->sml_type );
917                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
918                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
919                         ber_dupbv( &mod->sml_values[0], &timestamp );
920                         BER_BVZERO( &mod->sml_values[1] );
921                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
922                         mod->sml_nvalues = NULL;
923                         *modtail = mod;
924                         modtail = &mod->sml_next;
925                 }
926         }
927 }
928