]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
more on manage access level
[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_managing = 0;
110                 mod->sml_next = NULL;
111                 *modtail = mod;
112
113                 switch( mop ) {
114                 case LDAP_MOD_ADD:
115                         if ( mod->sml_values == NULL ) {
116                                 Debug( LDAP_DEBUG_ANY,
117                                         "do_modify: modify/add operation (%ld) requires values\n",
118                                         (long) mop, 0, 0 );
119
120                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
121                                         "modify/add operation requires values" );
122                                 goto cleanup;
123                         }
124
125                         /* fall through */
126
127                 case LDAP_MOD_DELETE:
128                 case LDAP_MOD_REPLACE:
129                         break;
130
131                 case LDAP_MOD_INCREMENT:
132                         if( op->o_protocol >= LDAP_VERSION3 ) {
133                                 increment++;
134                                 if ( mod->sml_values == NULL ) {
135                                         Debug( LDAP_DEBUG_ANY, "do_modify: "
136                                                 "modify/increment operation (%ld) requires value\n",
137                                                 (long) mop, 0, 0 );
138
139                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
140                                                 "modify/increment operation requires value" );
141                                         goto cleanup;
142                                 }
143
144                                 if ( !BER_BVISNULL( &mod->sml_values[ 1 ] ) ) {
145                                         Debug( LDAP_DEBUG_ANY, "do_modify: modify/increment "
146                                                 "operation (%ld) requires single value\n",
147                                                 (long) mop, 0, 0 );
148
149                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
150                                                 "modify/increment operation requires single value" );
151                                         goto cleanup;
152                                 }
153
154                                 break;
155                         }
156                         /* fall thru */
157
158                 default: {
159                                 Debug( LDAP_DEBUG_ANY,
160                                         "do_modify: unrecognized modify operation (%ld)\n",
161                                         (long) mop, 0, 0 );
162
163                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
164                                         "unrecognized modify operation" );
165                                 goto cleanup;
166                         }
167                 }
168
169                 modtail = &mod->sml_next;
170         }
171         *modtail = NULL;
172
173         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
174                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
175
176                 goto cleanup;
177         }
178
179         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
180                 op->o_tmpmemctx );
181         if( rs->sr_err != LDAP_SUCCESS ) {
182                 Debug( LDAP_DEBUG_ANY,
183                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
184                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
185                 goto cleanup;
186         }
187
188         rs->sr_err = slap_mods_check( modlist,
189                 &rs->sr_text, textbuf, textlen, NULL );
190
191         if ( rs->sr_err != LDAP_SUCCESS ) {
192                 send_ldap_result( op, rs );
193                 goto cleanup;
194         }
195
196         /* FIXME: needs review */
197         op->orm_modlist = modlist;
198         op->orm_increment = increment;
199
200         op->o_bd = frontendDB;
201         rs->sr_err = frontendDB->be_modify( op, rs );
202
203 cleanup:
204         slap_graduate_commit_csn( op );
205
206         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
207         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
208         if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
209
210         return rs->sr_err;
211 }
212
213 int
214 fe_op_modify( Operation *op, SlapReply *rs )
215 {
216 #ifdef LDAP_DEBUG
217         Modifications   *tmp;
218 #endif
219         int             manageDSAit;
220         Modifications   *modlist = op->orm_modlist;
221         Modifications   **modtail = &modlist;
222         int             increment = op->orm_increment;
223         BackendDB *op_be;
224         char            textbuf[ SLAP_TEXT_BUFLEN ];
225         size_t          textlen = sizeof( textbuf );
226         
227         if( op->o_req_ndn.bv_len == 0 ) {
228                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
229
230                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
231                         "modify upon the root DSE not supported" );
232                 goto cleanup;
233
234         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
235                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
236
237                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
238                         "modification of subschema subentry not supported" );
239                 goto cleanup;
240         }
241
242 #ifdef LDAP_DEBUG
243         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
244
245         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
246                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
247                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
248                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
249                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
250                                         "replace")), tmp->sml_type.bv_val, 0 );
251
252                 if ( tmp->sml_values == NULL ) {
253                         Debug( LDAP_DEBUG_ARGS, "%s\n",
254                            "\t\tno values", NULL, NULL );
255                 } else if ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
256                         Debug( LDAP_DEBUG_ARGS, "%s\n",
257                            "\t\tzero values", NULL, NULL );
258                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
259                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
260                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
261                 } else {
262                         Debug( LDAP_DEBUG_ARGS, "%s\n",
263                            "\t\tmultiple values", NULL, NULL );
264                 }
265         }
266
267         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
268                 char abuf[BUFSIZ/2], *ptr = abuf;
269                 int len = 0;
270
271                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
272                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
273
274                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
275                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
276                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
277                                     op->o_log_prefix, abuf, 0, 0, 0 );
278
279                                 len = 0;
280                                 ptr = abuf;
281
282                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
283                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
284                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
285                                         continue;
286                                 }
287                         }
288                         if (len) {
289                                 *ptr++ = ' ';
290                                 len++;
291                         }
292                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
293                         len += tmp->sml_type.bv_len;
294                 }
295                 if (len) {
296                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
297                                 op->o_log_prefix, abuf, 0, 0, 0 );
298                 }
299         }
300 #endif  /* LDAP_DEBUG */
301
302         manageDSAit = get_manageDSAit( op );
303
304         /*
305          * We could be serving multiple database backends.  Select the
306          * appropriate one, or send a referral to our "referral server"
307          * if we don't hold it.
308          */
309         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
310         if ( op->o_bd == NULL ) {
311                 rs->sr_ref = referral_rewrite( default_referral,
312                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
313                 if (!rs->sr_ref) rs->sr_ref = default_referral;
314
315                 if (rs->sr_ref != NULL ) {
316                         rs->sr_err = LDAP_REFERRAL;
317                         op->o_bd = frontendDB;
318                         send_ldap_result( op, rs );
319                         op->o_bd = NULL;
320
321                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
322                 } else {
323                         op->o_bd = frontendDB;
324                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
325                                 "no global superior knowledge" );
326                         op->o_bd = NULL;
327                 }
328                 goto cleanup;
329         }
330
331         /* If we've got a glued backend, check the real backend */
332         op_be = op->o_bd;
333         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
334                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
335         }
336
337         /* check restrictions */
338         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
339                 send_ldap_result( op, rs );
340                 goto cleanup;
341         }
342
343         /* check for referrals */
344         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
345                 goto cleanup;
346         }
347
348         rs->sr_err = slap_mods_obsolete_check( op, modlist,
349                 &rs->sr_text, textbuf, textlen );
350         if ( rs->sr_err != LDAP_SUCCESS ) {
351                 send_ldap_result( op, rs );
352                 goto cleanup;
353         }
354
355         /* check for modify/increment support */
356         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
357                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
358                         "modify/increment not supported in context" );
359         }
360
361         /*
362          * do the modify if 1 && (2 || 3)
363          * 1) there is a modify function implemented in this backend;
364          * 2) this backend is master for what it holds;
365          * 3) it's a replica and the dn supplied is the update_ndn.
366          */
367         if ( op->o_bd->be_modify ) {
368                 /* do the update here */
369                 int repl_user = be_isupdate( op );
370
371                 /* Multimaster slapd does not have to check for replicator dn
372                  * because it accepts each modify request
373                  */
374 #ifndef SLAPD_MULTIMASTER
375                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
376 #endif
377                 {
378                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
379                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
380
381                         op->o_bd = op_be;
382
383                         if ( !update ) {
384                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
385                                         &rs->sr_text, textbuf, textlen );
386                                 if ( rs->sr_err != LDAP_SUCCESS ) {
387                                         send_ldap_result( op, rs );
388                                         goto cleanup;
389                                 }
390                         }
391
392                         if ( !repl_user ) {
393                                 for( modtail = &modlist;
394                                         *modtail != NULL;
395                                         modtail = &(*modtail)->sml_next )
396                                 {
397                                         /* empty */
398                                 }
399
400                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
401                                         &rs->sr_text, textbuf, textlen, 1 );
402                                 if( rs->sr_err != LDAP_SUCCESS ) {
403                                         send_ldap_result( op, rs );
404                                         goto cleanup;
405                                 }
406                         }
407
408                         op->orm_modlist = modlist;
409 #ifdef SLAPD_MULTIMASTER
410                         if ( !repl_user )
411 #endif
412                         {
413                                 /* but multimaster slapd logs only the ones 
414                                  * not from a replicator user */
415                                 cb.sc_next = op->o_callback;
416                                 op->o_callback = &cb;
417                         }
418                         op->o_bd->be_modify( op, rs );
419
420 #ifndef SLAPD_MULTIMASTER
421                 /* send a referral */
422                 } else {
423                         BerVarray defref = op->o_bd->be_update_refs
424                                 ? op->o_bd->be_update_refs : default_referral;
425                         if ( defref != NULL ) {
426                                 rs->sr_ref = referral_rewrite( defref,
427                                         NULL, &op->o_req_dn,
428                                         LDAP_SCOPE_DEFAULT );
429                                 if ( rs->sr_ref == NULL ) {
430                                         /* FIXME: must duplicate, because
431                                          * overlays may muck with it */
432                                         rs->sr_ref = defref;
433                                 }
434                                 rs->sr_err = LDAP_REFERRAL;
435                                 send_ldap_result( op, rs );
436                                 if ( rs->sr_ref != defref ) {
437                                         ber_bvarray_free( rs->sr_ref );
438                                 }
439
440                         } else {
441                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
442                                         "shadow context; no update referral" );
443                         }
444 #endif
445                 }
446         } else {
447                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
448                     "operation not supported within namingContext" );
449         }
450
451 cleanup:;
452         return rs->sr_err;
453 }
454
455 /*
456  * Obsolete constraint checking.
457  */
458 int
459 slap_mods_obsolete_check(
460         Operation *op,
461         Modifications *ml,
462         const char **text,
463         char *textbuf,
464         size_t textlen )
465 {
466         if( get_manageDIT( op ) ) return LDAP_SUCCESS;
467
468         for ( ; ml != NULL; ml = ml->sml_next ) {
469                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
470                         (( ml->sml_op != LDAP_MOD_REPLACE &&
471                                 ml->sml_op != LDAP_MOD_DELETE ) ||
472                                         ml->sml_values != NULL ))
473                 {
474                         /*
475                          * attribute is obsolete,
476                          * only allow replace/delete with no values
477                          */
478                         snprintf( textbuf, textlen,
479                                 "%s: attribute is obsolete",
480                                 ml->sml_type.bv_val );
481                         *text = textbuf;
482                         return LDAP_CONSTRAINT_VIOLATION;
483                 }
484         }
485
486         return LDAP_SUCCESS;
487 }
488
489 /*
490  * No-user-modification constraint checking.
491  */
492 int
493 slap_mods_no_user_mod_check(
494         Operation *op,
495         Modifications *ml,
496         const char **text,
497         char *textbuf,
498         size_t textlen )
499 {
500         for ( ; ml != NULL; ml = ml->sml_next ) {
501                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) continue;
502
503                 if ( get_manageDIT( op ) ) {
504                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
505                                 ml->sml_managing = 1;
506                                 continue;
507                         }
508
509                         /* attribute not manageable */
510                         snprintf( textbuf, textlen,
511                                 "%s: no-user-modification attribute not manageable",
512                                 ml->sml_type.bv_val );
513
514                 } else {
515                         /* user modification disallowed */
516                         snprintf( textbuf, textlen,
517                                 "%s: no user modification allowed",
518                                 ml->sml_type.bv_val );
519                 }
520
521                 *text = textbuf;
522                 return LDAP_CONSTRAINT_VIOLATION;
523         }
524
525         return LDAP_SUCCESS;
526 }
527
528 int
529 slap_mods_no_repl_user_mod_check(
530         Operation *op,
531         Modifications *ml,
532         const char **text,
533         char *textbuf,
534         size_t textlen )
535 {
536         Modifications *mods;
537         Modifications *modp;
538
539         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
540                 assert( mods->sml_op == LDAP_MOD_ADD );
541
542                 /* check doesn't already appear */
543                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
544                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
545                                 snprintf( textbuf, textlen,
546                                         "attribute '%s' provided more than once",
547                                         mods->sml_desc->ad_cname.bv_val );
548                                 return LDAP_TYPE_OR_VALUE_EXISTS;
549                         }
550                 }
551         }
552
553         return LDAP_SUCCESS;
554 }
555
556 /*
557  * Do basic attribute type checking and syntax validation.
558  */
559 int slap_mods_check(
560         Modifications *ml,
561         const char **text,
562         char *textbuf,
563         size_t textlen,
564         void *ctx )
565 {
566         int rc;
567
568         for( ; ml != NULL; ml = ml->sml_next ) {
569                 AttributeDescription *ad = NULL;
570
571                 /* convert to attribute description */
572                 if ( ml->sml_desc == NULL ) {
573                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
574                         if( rc != LDAP_SUCCESS ) {
575                                 snprintf( textbuf, textlen, "%s: %s",
576                                         ml->sml_type.bv_val, *text );
577                                 *text = textbuf;
578                                 return rc;
579                         }
580                 }
581
582                 ad = ml->sml_desc;
583
584                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
585                         && !slap_ad_is_binary( ad ))
586                 {
587                         /* attribute requires binary transfer */
588                         snprintf( textbuf, textlen,
589                                 "%s: requires ;binary transfer",
590                                 ml->sml_type.bv_val );
591                         *text = textbuf;
592                         return LDAP_UNDEFINED_TYPE;
593                 }
594
595                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
596                         && slap_ad_is_binary( ad ))
597                 {
598                         /* attribute does not require binary transfer */
599                         snprintf( textbuf, textlen,
600                                 "%s: disallows ;binary transfer",
601                                 ml->sml_type.bv_val );
602                         *text = textbuf;
603                         return LDAP_UNDEFINED_TYPE;
604                 }
605
606                 if( slap_ad_is_tag_range( ad )) {
607                         /* attribute requires binary transfer */
608                         snprintf( textbuf, textlen,
609                                 "%s: inappropriate use of tag range option",
610                                 ml->sml_type.bv_val );
611                         *text = textbuf;
612                         return LDAP_UNDEFINED_TYPE;
613                 }
614
615 #if 0
616                 if ( is_at_obsolete( ad->ad_type ) &&
617                         (( ml->sml_op != LDAP_MOD_REPLACE &&
618                                 ml->sml_op != LDAP_MOD_DELETE ) ||
619                                         ml->sml_values != NULL ))
620                 {
621                         /*
622                          * attribute is obsolete,
623                          * only allow replace/delete with no values
624                          */
625                         snprintf( textbuf, textlen,
626                                 "%s: attribute is obsolete",
627                                 ml->sml_type.bv_val );
628                         *text = textbuf;
629                         return LDAP_CONSTRAINT_VIOLATION;
630                 }
631 #endif
632
633                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
634 #ifdef SLAPD_REAL_SYNTAX
635                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
636 #endif
637                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
638                 {
639                         /*
640                          * attribute values must be INTEGER or REAL
641                          */
642                         snprintf( textbuf, textlen,
643                                 "%s: attribute syntax inappropriate for increment",
644                                 ml->sml_type.bv_val );
645                         *text = textbuf;
646                         return LDAP_CONSTRAINT_VIOLATION;
647                 }
648
649                 /*
650                  * check values
651                  */
652                 if( ml->sml_values != NULL ) {
653                         ber_len_t nvals;
654                         slap_syntax_validate_func *validate =
655                                 ad->ad_type->sat_syntax->ssyn_validate;
656                         slap_syntax_transform_func *pretty =
657                                 ad->ad_type->sat_syntax->ssyn_pretty;
658  
659                         if( !pretty && !validate ) {
660                                 *text = "no validator for syntax";
661                                 snprintf( textbuf, textlen,
662                                         "%s: no validator for syntax %s",
663                                         ml->sml_type.bv_val,
664                                         ad->ad_type->sat_syntax->ssyn_oid );
665                                 *text = textbuf;
666                                 return LDAP_INVALID_SYNTAX;
667                         }
668
669                         /*
670                          * check that each value is valid per syntax
671                          *      and pretty if appropriate
672                          */
673                         for ( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
674                                 struct berval pval;
675
676                                 if ( pretty ) {
677 #ifdef SLAP_ORDERED_PRETTYNORM
678                                         rc = ordered_value_pretty( ad,
679                                                 &ml->sml_values[nvals], &pval, ctx );
680 #else /* ! SLAP_ORDERED_PRETTYNORM */
681                                         rc = pretty( ad->ad_type->sat_syntax,
682                                                 &ml->sml_values[nvals], &pval, ctx );
683 #endif /* ! SLAP_ORDERED_PRETTYNORM */
684                                 } else {
685 #ifdef SLAP_ORDERED_PRETTYNORM
686                                         rc = ordered_value_validate( ad,
687                                                 &ml->sml_values[nvals] );
688 #else /* ! SLAP_ORDERED_PRETTYNORM */
689                                         rc = validate( ad->ad_type->sat_syntax,
690                                                 &ml->sml_values[nvals] );
691 #endif /* ! SLAP_ORDERED_PRETTYNORM */
692                                 }
693
694                                 if( rc != 0 ) {
695                                         snprintf( textbuf, textlen,
696                                                 "%s: value #%ld invalid per syntax",
697                                                 ml->sml_type.bv_val, (long) nvals );
698                                         *text = textbuf;
699                                         return LDAP_INVALID_SYNTAX;
700                                 }
701
702                                 if( pretty ) {
703                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
704                                         ml->sml_values[nvals] = pval;
705                                 }
706                         }
707
708                         /*
709                          * a rough single value check... an additional check is needed
710                          * to catch add of single value to existing single valued attribute
711                          */
712                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
713                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
714                         {
715                                 snprintf( textbuf, textlen,
716                                         "%s: multiple values provided",
717                                         ml->sml_type.bv_val );
718                                 *text = textbuf;
719                                 return LDAP_CONSTRAINT_VIOLATION;
720                         }
721
722                         /* if the type has a normalizer, generate the
723                          * normalized values. otherwise leave them NULL.
724                          *
725                          * this is different from the rule for attributes
726                          * in an entry - in an attribute list, the normalized
727                          * value is set equal to the non-normalized value
728                          * when there is no normalizer.
729                          */
730                         if( nvals && ad->ad_type->sat_equality &&
731                                 ad->ad_type->sat_equality->smr_normalize )
732                         {
733                                 ml->sml_nvalues = ber_memalloc_x(
734                                         (nvals+1)*sizeof(struct berval), ctx );
735
736                                 for ( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
737 #ifdef SLAP_ORDERED_PRETTYNORM
738                                         rc = ordered_value_normalize(
739                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
740                                                 ad,
741                                                 ad->ad_type->sat_equality,
742                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
743 #else /* ! SLAP_ORDERED_PRETTYNORM */
744                                         rc = ad->ad_type->sat_equality->smr_normalize(
745                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
746                                                 ad->ad_type->sat_syntax,
747                                                 ad->ad_type->sat_equality,
748                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
749 #endif /* ! SLAP_ORDERED_PRETTYNORM */
750                                         if ( rc ) {
751                                                 Debug( LDAP_DEBUG_ANY,
752                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
753                                                         rc, 0, 0 );
754                                                 snprintf( textbuf, textlen,
755                                                         "%s: value #%ld normalization failed",
756                                                         ml->sml_type.bv_val, (long) nvals );
757                                                 *text = textbuf;
758                                                 return rc;
759                                         }
760                                 }
761
762                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
763                         }
764
765                         /* check for duplicates, but ignore Deletes.
766                          */
767                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
768                                 int             i, j, rc, match;
769                                 MatchingRule *mr = ad->ad_type->sat_equality;
770
771                                 for ( i = 1; i < nvals ; i++ ) {
772                                         /* test asserted values against themselves */
773                                         for( j = 0; j < i; j++ ) {
774                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
775                                                         SLAP_MR_EQUALITY
776                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
777                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
778                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
779                                                         ml->sml_nvalues
780                                                                 ? &ml->sml_nvalues[i]
781                                                                 : &ml->sml_values[i],
782                                                         ml->sml_nvalues
783                                                                 ? &ml->sml_nvalues[j]
784                                                                 : &ml->sml_values[j],
785                                                         text );
786                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
787                                                         /* value exists already */
788                                                         snprintf( textbuf, textlen,
789                                                                 "%s: value #%d provided more than once",
790                                                                 ml->sml_desc->ad_cname.bv_val, j );
791                                                         *text = textbuf;
792                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
793
794                                                 } else if ( rc != LDAP_SUCCESS ) {
795                                                         return rc;
796                                                 }
797                                         }
798                                 }
799                         }
800
801                 }
802         }
803
804         return LDAP_SUCCESS;
805 }
806
807 /* Enter with bv->bv_len = sizeof buffer, returns with
808  * actual length of string
809  */
810 void slap_timestamp( time_t *tm, struct berval *bv )
811 {
812         struct tm *ltm;
813 #ifdef HAVE_GMTIME_R
814         struct tm ltm_buf;
815
816         ltm = gmtime_r( tm, &ltm_buf );
817 #else
818         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
819         ltm = gmtime( tm );
820 #endif
821
822         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
823
824 #ifndef HAVE_GMTIME_R
825         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
826 #endif
827 }
828
829 int slap_mods_opattrs(
830         Operation *op,
831         Modifications *mods,
832         Modifications **modtail,
833         const char **text,
834         char *textbuf, size_t textlen,
835         int manage_ctxcsn )
836 {
837         struct berval name, timestamp, csn;
838         struct berval nname;
839         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
840         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
841         Modifications *mod;
842
843         int mop = op->o_tag == LDAP_REQ_ADD
844                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
845
846         assert( modtail != NULL );
847         assert( *modtail == NULL );
848
849         if ( SLAP_LASTMOD( op->o_bd ) ) {
850                 time_t now = slap_get_time();
851
852                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, manage_ctxcsn );
853
854                 timestamp.bv_val = timebuf;
855                 timestamp.bv_len = sizeof(timebuf);
856
857                 slap_timestamp( &now, &timestamp );
858
859                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
860                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
861                         nname = name;
862                 } else {
863                         name = op->o_dn;
864                         nname = op->o_ndn;
865                 }
866         }
867
868         if ( op->o_tag == LDAP_REQ_ADD ) {
869                 struct berval tmpval;
870
871                 mod = *modtail;
872                 if ( get_manageDIT( op ) ) {
873                         for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
874                                 if ( mod->sml_desc == slap_schema.si_ad_structuralObjectClass ) {
875                                         break;
876                                 }
877                         }
878
879                 }
880
881                 if ( mod == *modtail ) {
882                         int rc = mods_structural_class( mods, &tmpval,
883                                 text, textbuf, textlen );
884                         if( rc != LDAP_SUCCESS ) return rc;
885
886                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
887                         mod->sml_managing = 0;
888                         mod->sml_op = mop;
889                         mod->sml_flags = SLAP_MOD_INTERNAL;
890                         BER_BVZERO( &mod->sml_type );
891                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
892                         mod->sml_values =
893                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
894                         ber_dupbv( &mod->sml_values[0], &tmpval );
895                         BER_BVZERO( &mod->sml_values[1] );
896                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
897                         mod->sml_nvalues =
898                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
899                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
900                         BER_BVZERO( &mod->sml_nvalues[1] );
901                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
902                         *modtail = mod;
903                         modtail = &mod->sml_next;
904                 }
905
906                 if ( SLAP_LASTMOD( op->o_bd ) ) {
907                         mod = *modtail;
908                         if ( get_manageDIT( op ) ) {
909                                 for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
910                                         if ( mod->sml_desc == slap_schema.si_ad_entryUUID ) {
911                                                 break;
912                                         }
913                                 }
914                         }
915
916                         if ( mod == *modtail ) {
917                                 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
918
919                                 tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
920                                 tmpval.bv_val = uuidbuf;
921                         
922                                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
923                                 mod->sml_managing = 0;
924                                 mod->sml_op = mop;
925                                 mod->sml_flags = SLAP_MOD_INTERNAL;
926                                 BER_BVZERO( &mod->sml_type );
927                                 mod->sml_desc = slap_schema.si_ad_entryUUID;
928                                 mod->sml_values =
929                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
930                                 ber_dupbv( &mod->sml_values[0], &tmpval );
931                                 BER_BVZERO( &mod->sml_values[1] );
932                                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
933                                 mod->sml_nvalues =
934                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
935                                 (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
936                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
937                                                 mod->sml_desc->ad_type->sat_syntax,
938                                                 mod->sml_desc->ad_type->sat_equality,
939                                                 mod->sml_values, mod->sml_nvalues, NULL );
940                                 BER_BVZERO( &mod->sml_nvalues[1] );
941                                 *modtail = mod;
942                                 modtail = &mod->sml_next;
943                         }
944
945                         mod = *modtail;
946                         if ( get_manageDIT( op ) ) {
947                                 for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
948                                         if ( mod->sml_desc == slap_schema.si_ad_creatorsName ) {
949                                                 break;
950                                         }
951                                 }
952                         }
953
954                         if ( mod == *modtail ) {
955                                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
956                                 mod->sml_managing = 0;
957                                 mod->sml_op = mop;
958                                 mod->sml_flags = SLAP_MOD_INTERNAL;
959                                 BER_BVZERO( &mod->sml_type );
960                                 mod->sml_desc = slap_schema.si_ad_creatorsName;
961                                 mod->sml_values =
962                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
963                                 ber_dupbv( &mod->sml_values[0], &name );
964                                 BER_BVZERO( &mod->sml_values[1] );
965                                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
966                                 mod->sml_nvalues =
967                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
968                                 ber_dupbv( &mod->sml_nvalues[0], &nname );
969                                 BER_BVZERO( &mod->sml_nvalues[1] );
970                                 assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
971                                 *modtail = mod;
972                                 modtail = &mod->sml_next;
973                         }
974         
975                         mod = *modtail;
976                         if ( get_manageDIT( op ) ) {
977                                 for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
978                                         if ( mod->sml_desc == slap_schema.si_ad_createTimestamp ) {
979                                                 break;
980                                         }
981                                 }
982                         }
983
984                         if ( mod == *modtail ) {
985                                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
986                                 mod->sml_managing = 0;
987                                 mod->sml_op = mop;
988                                 mod->sml_flags = SLAP_MOD_INTERNAL;
989                                 BER_BVZERO( &mod->sml_type );
990                                 mod->sml_desc = slap_schema.si_ad_createTimestamp;
991                                 mod->sml_values =
992                                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
993                                 ber_dupbv( &mod->sml_values[0], &timestamp );
994                                 BER_BVZERO( &mod->sml_values[1] );
995                                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
996                                 mod->sml_nvalues = NULL;
997                                 *modtail = mod;
998                                 modtail = &mod->sml_next;
999                         }
1000                 }
1001         }
1002
1003         if ( SLAP_LASTMOD( op->o_bd ) ) {
1004                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1005                 mod->sml_managing = 0;
1006                 mod->sml_op = mop;
1007                 mod->sml_flags = SLAP_MOD_INTERNAL;
1008                 BER_BVZERO( &mod->sml_type );
1009                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1010                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1011                 ber_dupbv( &mod->sml_values[0], &csn );
1012                 BER_BVZERO( &mod->sml_values[1] );
1013                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1014                 mod->sml_nvalues = NULL;
1015                 *modtail = mod;
1016                 modtail = &mod->sml_next;
1017         
1018                 mod = *modtail;
1019                 if ( get_manageDIT( op ) ) {
1020                         for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
1021                                 if ( mod->sml_desc == slap_schema.si_ad_modifiersName ) {
1022                                         break;
1023                                 }
1024                         }
1025                 }
1026
1027                 if ( mod == *modtail ) {
1028                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1029                         mod->sml_managing = 0;
1030                         mod->sml_op = mop;
1031                         mod->sml_flags = SLAP_MOD_INTERNAL;
1032                         BER_BVZERO( &mod->sml_type );
1033                         mod->sml_desc = slap_schema.si_ad_modifiersName;
1034                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1035                         ber_dupbv( &mod->sml_values[0], &name );
1036                         BER_BVZERO( &mod->sml_values[1] );
1037                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1038                         mod->sml_nvalues =
1039                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1040                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1041                         BER_BVZERO( &mod->sml_nvalues[1] );
1042                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
1043                         *modtail = mod;
1044                         modtail = &mod->sml_next;
1045                 }
1046
1047                 mod = *modtail;
1048                 if ( get_manageDIT( op ) ) {
1049                         for ( mod = mods; mod != *modtail; mod = mod->sml_next ) {
1050                                 if ( mod->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
1051                                         break;
1052                                 }
1053                         }
1054                 }
1055
1056                 if ( mod == *modtail ) {
1057                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1058                         mod->sml_managing = 0;
1059                         mod->sml_op = mop;
1060                         mod->sml_flags = SLAP_MOD_INTERNAL;
1061                         BER_BVZERO( &mod->sml_type );
1062                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1063                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1064                         ber_dupbv( &mod->sml_values[0], &timestamp );
1065                         BER_BVZERO( &mod->sml_values[1] );
1066                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1067                         mod->sml_nvalues = NULL;
1068                         *modtail = mod;
1069                         modtail = &mod->sml_next;
1070                 }
1071         }
1072
1073         *modtail = NULL;
1074         return LDAP_SUCCESS;
1075 }
1076