]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Silence valgrind warnings
[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         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, 1 );
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 ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
255                         Debug( LDAP_DEBUG_ARGS, "%s\n",
256                            "\t\tzero values", NULL, NULL );
257                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
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         rs->sr_err = slap_mods_obsolete_check( op, modlist,
348                 &rs->sr_text, textbuf, textlen );
349         if ( rs->sr_err != LDAP_SUCCESS ) {
350                 send_ldap_result( op, rs );
351                 goto cleanup;
352         }
353
354         /* check for modify/increment support */
355         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
356                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
357                         "modify/increment not supported in context" );
358         }
359
360         /*
361          * do the modify if 1 && (2 || 3)
362          * 1) there is a modify function implemented in this backend;
363          * 2) this backend is master for what it holds;
364          * 3) it's a replica and the dn supplied is the update_ndn.
365          */
366         if ( op->o_bd->be_modify ) {
367                 /* do the update here */
368                 int repl_user = be_isupdate( op );
369
370                 /* Multimaster slapd does not have to check for replicator dn
371                  * because it accepts each modify request
372                  */
373 #ifndef SLAPD_MULTIMASTER
374                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
375 #endif
376                 {
377                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
378                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
379
380                         op->o_bd = op_be;
381
382                         if ( !update ) {
383                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
384                                         &rs->sr_text, textbuf, textlen );
385                                 if ( rs->sr_err != LDAP_SUCCESS ) {
386                                         send_ldap_result( op, rs );
387                                         goto cleanup;
388                                 }
389                         }
390
391                         if ( !repl_user ) {
392                                 for( modtail = &modlist;
393                                         *modtail != NULL;
394                                         modtail = &(*modtail)->sml_next )
395                                 {
396                                         /* empty */
397                                 }
398
399                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
400                                         &rs->sr_text, textbuf, textlen, 1 );
401                                 if( rs->sr_err != LDAP_SUCCESS ) {
402                                         send_ldap_result( op, rs );
403                                         goto cleanup;
404                                 }
405                         }
406
407                         op->orm_modlist = modlist;
408 #ifdef SLAPD_MULTIMASTER
409                         if ( !repl_user )
410 #endif
411                         {
412                                 /* but multimaster slapd logs only the ones 
413                                  * not from a replicator user */
414                                 cb.sc_next = op->o_callback;
415                                 op->o_callback = &cb;
416                         }
417                         op->o_bd->be_modify( op, rs );
418
419 #ifndef SLAPD_MULTIMASTER
420                 /* send a referral */
421                 } else {
422                         BerVarray defref = op->o_bd->be_update_refs
423                                 ? op->o_bd->be_update_refs : default_referral;
424                         if ( defref != NULL ) {
425                                 rs->sr_ref = referral_rewrite( defref,
426                                         NULL, &op->o_req_dn,
427                                         LDAP_SCOPE_DEFAULT );
428                                 if ( rs->sr_ref == NULL ) {
429                                         /* FIXME: must duplicate, because
430                                          * overlays may muck with it */
431                                         rs->sr_ref = defref;
432                                 }
433                                 rs->sr_err = LDAP_REFERRAL;
434                                 send_ldap_result( op, rs );
435                                 if ( rs->sr_ref != defref ) {
436                                         ber_bvarray_free( rs->sr_ref );
437                                 }
438
439                         } else {
440                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
441                                         "shadow context; no update referral" );
442                         }
443 #endif
444                 }
445         } else {
446                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
447                     "operation not supported within namingContext" );
448         }
449
450 cleanup:;
451         return rs->sr_err;
452 }
453
454 /*
455  * Obsolete constraint checking.
456  */
457 int
458 slap_mods_obsolete_check(
459         Operation *op,
460         Modifications *ml,
461         const char **text,
462         char *textbuf,
463         size_t textlen )
464 {
465         if( get_manageDIT( op ) ) return LDAP_SUCCESS;
466
467         for ( ; ml != NULL; ml = ml->sml_next ) {
468                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
469                         (( ml->sml_op != LDAP_MOD_REPLACE &&
470                                 ml->sml_op != LDAP_MOD_DELETE ) ||
471                                         ml->sml_values != NULL ))
472                 {
473                         /*
474                          * attribute is obsolete,
475                          * only allow replace/delete with no values
476                          */
477                         snprintf( textbuf, textlen,
478                                 "%s: attribute is obsolete",
479                                 ml->sml_type.bv_val );
480                         *text = textbuf;
481                         return LDAP_CONSTRAINT_VIOLATION;
482                 }
483         }
484
485         return LDAP_SUCCESS;
486 }
487
488 /*
489  * No-user-modification constraint checking.
490  */
491 int
492 slap_mods_no_user_mod_check(
493         Operation *op,
494         Modifications *ml,
495         const char **text,
496         char *textbuf,
497         size_t textlen )
498 {
499         for ( ; ml != NULL; ml = ml->sml_next ) {
500                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) continue;
501
502                 if ( get_manageDIT( op ) ) {
503                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
504                                 ml->sml_flags |= SLAP_MOD_MANAGING;
505                                 continue;
506                         }
507
508                         /* attribute not manageable */
509                         snprintf( textbuf, textlen,
510                                 "%s: no-user-modification attribute not manageable",
511                                 ml->sml_type.bv_val );
512
513                 } else {
514                         /* user modification disallowed */
515                         snprintf( textbuf, textlen,
516                                 "%s: no user modification allowed",
517                                 ml->sml_type.bv_val );
518                 }
519
520                 *text = textbuf;
521                 return LDAP_CONSTRAINT_VIOLATION;
522         }
523
524         return LDAP_SUCCESS;
525 }
526
527 int
528 slap_mods_no_repl_user_mod_check(
529         Operation *op,
530         Modifications *ml,
531         const char **text,
532         char *textbuf,
533         size_t textlen )
534 {
535         Modifications *mods;
536         Modifications *modp;
537
538         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
539                 assert( mods->sml_op == LDAP_MOD_ADD );
540
541                 /* check doesn't already appear */
542                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
543                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
544                                 snprintf( textbuf, textlen,
545                                         "attribute '%s' provided more than once",
546                                         mods->sml_desc->ad_cname.bv_val );
547                                 return LDAP_TYPE_OR_VALUE_EXISTS;
548                         }
549                 }
550         }
551
552         return LDAP_SUCCESS;
553 }
554
555 /*
556  * Do basic attribute type checking and syntax validation.
557  */
558 int slap_mods_check(
559         Modifications *ml,
560         const char **text,
561         char *textbuf,
562         size_t textlen,
563         void *ctx )
564 {
565         int rc;
566
567         for( ; ml != NULL; ml = ml->sml_next ) {
568                 AttributeDescription *ad = NULL;
569
570                 /* convert to attribute description */
571                 if ( ml->sml_desc == NULL ) {
572                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
573                         if( rc != LDAP_SUCCESS ) {
574                                 snprintf( textbuf, textlen, "%s: %s",
575                                         ml->sml_type.bv_val, *text );
576                                 *text = textbuf;
577                                 return rc;
578                         }
579                 }
580
581                 ad = ml->sml_desc;
582
583                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
584                         && !slap_ad_is_binary( ad ))
585                 {
586                         /* attribute requires binary transfer */
587                         snprintf( textbuf, textlen,
588                                 "%s: requires ;binary transfer",
589                                 ml->sml_type.bv_val );
590                         *text = textbuf;
591                         return LDAP_UNDEFINED_TYPE;
592                 }
593
594                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
595                         && slap_ad_is_binary( ad ))
596                 {
597                         /* attribute does not require binary transfer */
598                         snprintf( textbuf, textlen,
599                                 "%s: disallows ;binary transfer",
600                                 ml->sml_type.bv_val );
601                         *text = textbuf;
602                         return LDAP_UNDEFINED_TYPE;
603                 }
604
605                 if( slap_ad_is_tag_range( ad )) {
606                         /* attribute requires binary transfer */
607                         snprintf( textbuf, textlen,
608                                 "%s: inappropriate use of tag range option",
609                                 ml->sml_type.bv_val );
610                         *text = textbuf;
611                         return LDAP_UNDEFINED_TYPE;
612                 }
613
614 #if 0
615                 if ( is_at_obsolete( ad->ad_type ) &&
616                         (( ml->sml_op != LDAP_MOD_REPLACE &&
617                                 ml->sml_op != LDAP_MOD_DELETE ) ||
618                                         ml->sml_values != NULL ))
619                 {
620                         /*
621                          * attribute is obsolete,
622                          * only allow replace/delete with no values
623                          */
624                         snprintf( textbuf, textlen,
625                                 "%s: attribute is obsolete",
626                                 ml->sml_type.bv_val );
627                         *text = textbuf;
628                         return LDAP_CONSTRAINT_VIOLATION;
629                 }
630 #endif
631
632                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
633 #ifdef SLAPD_REAL_SYNTAX
634                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
635 #endif
636                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
637                 {
638                         /*
639                          * attribute values must be INTEGER or REAL
640                          */
641                         snprintf( textbuf, textlen,
642                                 "%s: attribute syntax inappropriate for increment",
643                                 ml->sml_type.bv_val );
644                         *text = textbuf;
645                         return LDAP_CONSTRAINT_VIOLATION;
646                 }
647
648                 /*
649                  * check values
650                  */
651                 if( ml->sml_values != NULL ) {
652                         ber_len_t nvals;
653                         slap_syntax_validate_func *validate =
654                                 ad->ad_type->sat_syntax->ssyn_validate;
655                         slap_syntax_transform_func *pretty =
656                                 ad->ad_type->sat_syntax->ssyn_pretty;
657  
658                         if( !pretty && !validate ) {
659                                 *text = "no validator for syntax";
660                                 snprintf( textbuf, textlen,
661                                         "%s: no validator for syntax %s",
662                                         ml->sml_type.bv_val,
663                                         ad->ad_type->sat_syntax->ssyn_oid );
664                                 *text = textbuf;
665                                 return LDAP_INVALID_SYNTAX;
666                         }
667
668                         /*
669                          * check that each value is valid per syntax
670                          *      and pretty if appropriate
671                          */
672                         for ( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
673                                 struct berval pval;
674
675                                 if ( pretty ) {
676 #ifdef SLAP_ORDERED_PRETTYNORM
677                                         rc = ordered_value_pretty( ad,
678                                                 &ml->sml_values[nvals], &pval, ctx );
679 #else /* ! SLAP_ORDERED_PRETTYNORM */
680                                         rc = pretty( ad->ad_type->sat_syntax,
681                                                 &ml->sml_values[nvals], &pval, ctx );
682 #endif /* ! SLAP_ORDERED_PRETTYNORM */
683                                 } else {
684 #ifdef SLAP_ORDERED_PRETTYNORM
685                                         rc = ordered_value_validate( ad,
686                                                 &ml->sml_values[nvals] );
687 #else /* ! SLAP_ORDERED_PRETTYNORM */
688                                         rc = validate( ad->ad_type->sat_syntax,
689                                                 &ml->sml_values[nvals] );
690 #endif /* ! SLAP_ORDERED_PRETTYNORM */
691                                 }
692
693                                 if( rc != 0 ) {
694                                         snprintf( textbuf, textlen,
695                                                 "%s: value #%ld invalid per syntax",
696                                                 ml->sml_type.bv_val, (long) nvals );
697                                         *text = textbuf;
698                                         return LDAP_INVALID_SYNTAX;
699                                 }
700
701                                 if( pretty ) {
702                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
703                                         ml->sml_values[nvals] = pval;
704                                 }
705                         }
706
707                         /*
708                          * a rough single value check... an additional check is needed
709                          * to catch add of single value to existing single valued attribute
710                          */
711                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
712                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
713                         {
714                                 snprintf( textbuf, textlen,
715                                         "%s: multiple values provided",
716                                         ml->sml_type.bv_val );
717                                 *text = textbuf;
718                                 return LDAP_CONSTRAINT_VIOLATION;
719                         }
720
721                         /* if the type has a normalizer, generate the
722                          * normalized values. otherwise leave them NULL.
723                          *
724                          * this is different from the rule for attributes
725                          * in an entry - in an attribute list, the normalized
726                          * value is set equal to the non-normalized value
727                          * when there is no normalizer.
728                          */
729                         if( nvals && ad->ad_type->sat_equality &&
730                                 ad->ad_type->sat_equality->smr_normalize )
731                         {
732                                 ml->sml_nvalues = ber_memalloc_x(
733                                         (nvals+1)*sizeof(struct berval), ctx );
734
735                                 for ( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
736 #ifdef SLAP_ORDERED_PRETTYNORM
737                                         rc = ordered_value_normalize(
738                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
739                                                 ad,
740                                                 ad->ad_type->sat_equality,
741                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
742 #else /* ! SLAP_ORDERED_PRETTYNORM */
743                                         rc = ad->ad_type->sat_equality->smr_normalize(
744                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
745                                                 ad->ad_type->sat_syntax,
746                                                 ad->ad_type->sat_equality,
747                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
748 #endif /* ! SLAP_ORDERED_PRETTYNORM */
749                                         if ( rc ) {
750                                                 Debug( LDAP_DEBUG_ANY,
751                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
752                                                         rc, 0, 0 );
753                                                 snprintf( textbuf, textlen,
754                                                         "%s: value #%ld normalization failed",
755                                                         ml->sml_type.bv_val, (long) nvals );
756                                                 *text = textbuf;
757                                                 return rc;
758                                         }
759                                 }
760
761                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
762                         }
763
764                         /* check for duplicates, but ignore Deletes.
765                          */
766                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
767                                 int             i, j, rc, match;
768                                 MatchingRule *mr = ad->ad_type->sat_equality;
769
770                                 for ( i = 1; i < nvals ; i++ ) {
771                                         /* test asserted values against themselves */
772                                         for( j = 0; j < i; j++ ) {
773                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
774                                                         SLAP_MR_EQUALITY
775                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
776                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
777                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
778                                                         ml->sml_nvalues
779                                                                 ? &ml->sml_nvalues[i]
780                                                                 : &ml->sml_values[i],
781                                                         ml->sml_nvalues
782                                                                 ? &ml->sml_nvalues[j]
783                                                                 : &ml->sml_values[j],
784                                                         text );
785                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
786                                                         /* value exists already */
787                                                         snprintf( textbuf, textlen,
788                                                                 "%s: value #%d provided more than once",
789                                                                 ml->sml_desc->ad_cname.bv_val, j );
790                                                         *text = textbuf;
791                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
792
793                                                 } else if ( rc != LDAP_SUCCESS ) {
794                                                         return rc;
795                                                 }
796                                         }
797                                 }
798                         }
799
800                 }
801         }
802
803         return LDAP_SUCCESS;
804 }
805
806 /* Enter with bv->bv_len = sizeof buffer, returns with
807  * actual length of string
808  */
809 void slap_timestamp( time_t *tm, struct berval *bv )
810 {
811         struct tm *ltm;
812 #ifdef HAVE_GMTIME_R
813         struct tm ltm_buf;
814
815         ltm = gmtime_r( tm, &ltm_buf );
816 #else
817         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
818         ltm = gmtime( tm );
819 #endif
820
821         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
822
823 #ifndef HAVE_GMTIME_R
824         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
825 #endif
826 }
827
828 int slap_mods_opattrs(
829         Operation *op,
830         Modifications *mods,
831         Modifications **modtail,
832         const char **text,
833         char *textbuf, size_t textlen,
834         int manage_ctxcsn )
835 {
836         struct berval name, timestamp, csn;
837         struct berval nname;
838         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
839         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
840         Modifications *mod;
841
842         int mop = op->o_tag == LDAP_REQ_ADD
843                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
844
845         assert( modtail != NULL );
846         assert( *modtail == NULL );
847
848         if ( SLAP_LASTMOD( op->o_bd ) ) {
849                 time_t now = slap_get_time();
850
851                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, manage_ctxcsn );
852
853                 timestamp.bv_val = timebuf;
854                 timestamp.bv_len = sizeof(timebuf);
855
856                 slap_timestamp( &now, &timestamp );
857
858                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
859                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
860                         nname = name;
861                 } else {
862                         name = op->o_dn;
863                         nname = op->o_ndn;
864                 }
865         }
866
867         if ( op->o_tag == LDAP_REQ_ADD ) {
868                 struct berval tmpval;
869
870                 mod = *modtail;
871                 if ( get_manageDIT( op ) ) {
872                         for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
873                                 if ( mod->sml_desc == slap_schema.si_ad_structuralObjectClass ) {
874                                         break;
875                                 }
876                         }
877
878                 }
879
880                 if ( mod == *modtail ) {
881                         int rc = mods_structural_class( mods, &tmpval,
882                                 text, textbuf, textlen );
883                         if( rc != LDAP_SUCCESS ) return rc;
884
885                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
886                         mod->sml_op = mop;
887                         mod->sml_flags = SLAP_MOD_INTERNAL;
888                         mod->sml_next = NULL;
889                         BER_BVZERO( &mod->sml_type );
890                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
891                         mod->sml_values =
892                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
893                         ber_dupbv( &mod->sml_values[0], &tmpval );
894                         BER_BVZERO( &mod->sml_values[1] );
895                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
896                         mod->sml_nvalues =
897                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
898                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
899                         BER_BVZERO( &mod->sml_nvalues[1] );
900                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
901                         *modtail = mod;
902                         modtail = &mod->sml_next;
903                 }
904
905                 if ( SLAP_LASTMOD( op->o_bd ) ) {
906                         mod = *modtail;
907                         if ( get_manageDIT( op ) ) {
908                                 for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
909                                         if ( mod->sml_desc == slap_schema.si_ad_entryUUID ) {
910                                                 break;
911                                         }
912                                 }
913                         }
914
915                         if ( mod == *modtail ) {
916                                 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
917
918                                 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
919                                 tmpval.bv_val = uuidbuf;
920                         
921                                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
922                                 mod->sml_op = mop;
923                                 mod->sml_flags = SLAP_MOD_INTERNAL;
924                                 mod->sml_next = NULL;
925                                 BER_BVZERO( &mod->sml_type );
926                                 mod->sml_desc = slap_schema.si_ad_entryUUID;
927                                 mod->sml_values =
928                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
929                                 ber_dupbv( &mod->sml_values[0], &tmpval );
930                                 BER_BVZERO( &mod->sml_values[1] );
931                                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
932                                 mod->sml_nvalues =
933                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
934                                 (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
935                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
936                                                 mod->sml_desc->ad_type->sat_syntax,
937                                                 mod->sml_desc->ad_type->sat_equality,
938                                                 mod->sml_values, mod->sml_nvalues, NULL );
939                                 BER_BVZERO( &mod->sml_nvalues[1] );
940                                 *modtail = mod;
941                                 modtail = &mod->sml_next;
942                         }
943
944                         mod = *modtail;
945                         if ( get_manageDIT( op ) ) {
946                                 for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
947                                         if ( mod->sml_desc == slap_schema.si_ad_creatorsName ) {
948                                                 break;
949                                         }
950                                 }
951                         }
952
953                         if ( mod == *modtail ) {
954                                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
955                                 mod->sml_op = mop;
956                                 mod->sml_flags = SLAP_MOD_INTERNAL;
957                                 mod->sml_next = NULL;
958                                 BER_BVZERO( &mod->sml_type );
959                                 mod->sml_desc = slap_schema.si_ad_creatorsName;
960                                 mod->sml_values =
961                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
962                                 ber_dupbv( &mod->sml_values[0], &name );
963                                 BER_BVZERO( &mod->sml_values[1] );
964                                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
965                                 mod->sml_nvalues =
966                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
967                                 ber_dupbv( &mod->sml_nvalues[0], &nname );
968                                 BER_BVZERO( &mod->sml_nvalues[1] );
969                                 assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
970                                 *modtail = mod;
971                                 modtail = &mod->sml_next;
972                         }
973         
974                         mod = *modtail;
975                         if ( get_manageDIT( op ) ) {
976                                 for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
977                                         if ( mod->sml_desc == slap_schema.si_ad_createTimestamp ) {
978                                                 break;
979                                         }
980                                 }
981                         }
982
983                         if ( mod == *modtail ) {
984                                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
985                                 mod->sml_op = mop;
986                                 mod->sml_flags = SLAP_MOD_INTERNAL;
987                                 mod->sml_next = NULL;
988                                 BER_BVZERO( &mod->sml_type );
989                                 mod->sml_desc = slap_schema.si_ad_createTimestamp;
990                                 mod->sml_values =
991                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
992                                 ber_dupbv( &mod->sml_values[0], &timestamp );
993                                 BER_BVZERO( &mod->sml_values[1] );
994                                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
995                                 mod->sml_nvalues = NULL;
996                                 *modtail = mod;
997                                 modtail = &mod->sml_next;
998                         }
999                 }
1000         }
1001
1002         if ( SLAP_LASTMOD( op->o_bd ) ) {
1003                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1004                 mod->sml_op = mop;
1005                 mod->sml_flags = SLAP_MOD_INTERNAL;
1006                 mod->sml_next = NULL;
1007                 BER_BVZERO( &mod->sml_type );
1008                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1009                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1010                 ber_dupbv( &mod->sml_values[0], &csn );
1011                 BER_BVZERO( &mod->sml_values[1] );
1012                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1013                 mod->sml_nvalues = NULL;
1014                 *modtail = mod;
1015                 modtail = &mod->sml_next;
1016         
1017                 mod = *modtail;
1018                 if ( get_manageDIT( op ) ) {
1019                         for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
1020                                 if ( mod->sml_desc == slap_schema.si_ad_modifiersName ) {
1021                                         break;
1022                                 }
1023                         }
1024                 }
1025
1026                 if ( mod == *modtail ) {
1027                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1028                         mod->sml_op = mop;
1029                         mod->sml_flags = SLAP_MOD_INTERNAL;
1030                         mod->sml_next = NULL;
1031                         BER_BVZERO( &mod->sml_type );
1032                         mod->sml_desc = slap_schema.si_ad_modifiersName;
1033                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1034                         ber_dupbv( &mod->sml_values[0], &name );
1035                         BER_BVZERO( &mod->sml_values[1] );
1036                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1037                         mod->sml_nvalues =
1038                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1039                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1040                         BER_BVZERO( &mod->sml_nvalues[1] );
1041                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
1042                         *modtail = mod;
1043                         modtail = &mod->sml_next;
1044                 }
1045
1046                 mod = *modtail;
1047                 if ( get_manageDIT( op ) ) {
1048                         for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
1049                                 if ( mod->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
1050                                         break;
1051                                 }
1052                         }
1053                 }
1054
1055                 if ( mod == *modtail ) {
1056                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1057                         mod->sml_op = mop;
1058                         mod->sml_flags = SLAP_MOD_INTERNAL;
1059                         mod->sml_next = NULL;
1060                         BER_BVZERO( &mod->sml_type );
1061                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1062                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1063                         ber_dupbv( &mod->sml_values[0], &timestamp );
1064                         BER_BVZERO( &mod->sml_values[1] );
1065                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1066                         mod->sml_nvalues = NULL;
1067                         *modtail = mod;
1068                         modtail = &mod->sml_next;
1069                 }
1070         }
1071
1072         *modtail = NULL;
1073         return LDAP_SUCCESS;
1074 }
1075