]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
fix schema definitions (duplicate objectClass OID still there)
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25
26 #include "ldap_pvt.h"
27 #include "slap.h"
28 #ifdef LDAP_SLAPI
29 #include "slapi.h"
30 #endif
31 #include "lutil.h"
32
33
34 int
35 do_modify(
36     Operation   *op,
37     SlapReply   *rs )
38 {
39         struct berval dn = { 0, NULL };
40         char            *last;
41         ber_tag_t       tag;
42         ber_len_t       len;
43         Modifications   *modlist = NULL;
44         Modifications   **modtail = &modlist;
45 #ifdef LDAP_DEBUG
46         Modifications *tmp;
47 #endif
48 #ifdef LDAP_SLAPI
49         LDAPMod         **modv = NULL;
50         Slapi_PBlock *pb = op->o_pb;
51 #endif
52         int manageDSAit;
53
54 #ifdef NEW_LOGGING
55         LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
56 #else
57         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
58 #endif
59
60         /*
61          * Parse the modify request.  It looks like this:
62          *
63          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
64          *              name    DistinguishedName,
65          *              mods    SEQUENCE OF SEQUENCE {
66          *                      operation       ENUMERATED {
67          *                              add     (0),
68          *                              delete  (1),
69          *                              replace (2)
70          *                      },
71          *                      modification    SEQUENCE {
72          *                              type    AttributeType,
73          *                              values  SET OF AttributeValue
74          *                      }
75          *              }
76          *      }
77          */
78
79         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
80 #ifdef NEW_LOGGING
81                 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
82 #else
83                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
84 #endif
85
86                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
87                 return SLAPD_DISCONNECT;
88         }
89
90 #ifdef NEW_LOGGING
91         LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
92 #else
93         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
94 #endif
95
96
97         /* collect modifications & save for later */
98
99         for ( tag = ber_first_element( op->o_ber, &len, &last );
100             tag != LBER_DEFAULT;
101             tag = ber_next_element( op->o_ber, &len, last ) )
102         {
103                 ber_int_t mop;
104                 Modifications tmp, *mod;
105
106                 tmp.sml_nvalues = NULL;
107
108                 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
109                     &tmp.sml_type, &tmp.sml_values )
110                     == LBER_ERROR )
111                 {
112                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding modlist error" );
113                         rs->sr_err = SLAPD_DISCONNECT;
114                         goto cleanup;
115                 }
116
117                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
118                 mod->sml_op = mop;
119                 mod->sml_type = tmp.sml_type;
120                 mod->sml_values = tmp.sml_values;
121                 mod->sml_nvalues = NULL;
122                 mod->sml_desc = NULL;
123                 mod->sml_next = NULL;
124                 *modtail = mod;
125
126                 switch( mop ) {
127                 case LDAP_MOD_ADD:
128                         if ( mod->sml_values == NULL ) {
129 #ifdef NEW_LOGGING
130                                 LDAP_LOG( OPERATION, ERR, 
131                                         "do_modify: modify/add operation (%ld) requires values\n",
132                                         (long)mop, 0, 0 );
133 #else
134                                 Debug( LDAP_DEBUG_ANY,
135                                         "do_modify: modify/add operation (%ld) requires values\n",
136                                         (long) mop, 0, 0 );
137 #endif
138
139                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
140                                         "modify/add operation requires values" );
141                                 goto cleanup;
142                         }
143
144                         /* fall through */
145
146                 case LDAP_MOD_DELETE:
147                 case LDAP_MOD_REPLACE:
148                         break;
149
150                 default: {
151 #ifdef NEW_LOGGING
152                                 LDAP_LOG( OPERATION, ERR, 
153                                         "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
154 #else
155                                 Debug( LDAP_DEBUG_ANY,
156                                         "do_modify: invalid modify operation (%ld)\n",
157                                         (long) mop, 0, 0 );
158 #endif
159
160                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
161                                         "unrecognized modify operation" );
162                                 goto cleanup;
163                         }
164                 }
165
166                 modtail = &mod->sml_next;
167         }
168         *modtail = NULL;
169
170         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
171 #ifdef NEW_LOGGING
172                 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
173 #else
174                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
175 #endif
176
177                 goto cleanup;
178         }
179
180         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
181         if( rs->sr_err != LDAP_SUCCESS ) {
182 #ifdef NEW_LOGGING
183                 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d  invalid dn (%s)\n",
184                         op->o_connid, dn.bv_val, 0 );
185 #else
186                 Debug( LDAP_DEBUG_ANY,
187                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
188 #endif
189                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
190                 goto cleanup;
191         }
192
193         if( op->o_req_ndn.bv_len == 0 ) {
194 #ifdef NEW_LOGGING
195                 LDAP_LOG( OPERATION, ERR, 
196                         "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
197 #else
198                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
199 #endif
200
201                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
202                         "modify upon the root DSE not supported" );
203                 goto cleanup;
204
205         } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
206 #ifdef NEW_LOGGING
207                 LDAP_LOG( OPERATION, ERR,
208                         "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0  );
209 #else
210                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
211 #endif
212
213                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
214                         "modification of subschema subentry not supported" );
215                 goto cleanup;
216         }
217
218 #ifdef LDAP_DEBUG
219 #ifdef NEW_LOGGING
220         LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0  );
221 #else
222         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
223 #endif
224
225         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
226 #ifdef NEW_LOGGING
227                 LDAP_LOG( OPERATION, DETAIL1, "\t%s:  %s\n", 
228                         tmp->sml_op == LDAP_MOD_ADD ?
229                         "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
230                         "delete" : "replace"), tmp->sml_type.bv_val, 0 );
231
232                 if ( tmp->sml_values == NULL ) {
233                         LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
234                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
235                         LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
236                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
237                         LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
238                 } else {
239                         LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
240                 }
241
242 #else
243                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
244                         tmp->sml_op == LDAP_MOD_ADD
245                                 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
246                                         ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
247
248                 if ( tmp->sml_values == NULL ) {
249                         Debug( LDAP_DEBUG_ARGS, "%s\n",
250                            "\t\tno values", NULL, NULL );
251                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
252                         Debug( LDAP_DEBUG_ARGS, "%s\n",
253                            "\t\tzero values", NULL, NULL );
254                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
255                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
256                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
257                 } else {
258                         Debug( LDAP_DEBUG_ARGS, "%s\n",
259                            "\t\tmultiple values", NULL, NULL );
260                 }
261 #endif
262         }
263
264         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
265                 char abuf[BUFSIZ/2], *ptr = abuf;
266                 int len = 0;
267
268                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
269                         op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
270
271                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
272                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
273                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
274                                     op->o_connid, op->o_opid, abuf, 0, 0 );
275                                 len = 0;
276                                 ptr = abuf;
277                         }
278                         if (len) {
279                                 *ptr++ = ' ';
280                                 len++;
281                         }
282                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
283                         len += tmp->sml_type.bv_len;
284                 }
285                 if (len) {
286                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
287                                 op->o_connid, op->o_opid, abuf, 0, 0 );
288                 }
289         }
290 #endif  /* LDAP_DEBUG */
291
292         manageDSAit = get_manageDSAit( op );
293
294         /*
295          * We could be serving multiple database backends.  Select the
296          * appropriate one, or send a referral to our "referral server"
297          * if we don't hold it.
298          */
299         if ( (op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 )) == NULL ) {
300                 rs->sr_ref = referral_rewrite( default_referral,
301                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
302                 if (!rs->sr_ref) rs->sr_ref = default_referral;
303
304                 rs->sr_err = LDAP_REFERRAL;
305                 send_ldap_result( op, rs );
306
307                 if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
308                 goto cleanup;
309         }
310
311         /* check restrictions */
312         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
313                 send_ldap_result( op, rs );
314                 goto cleanup;
315         }
316
317         /* check for referrals */
318         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
319                 goto cleanup;
320         }
321
322 #if defined( LDAP_SLAPI )
323         slapi_x_pblock_set_operation( pb, op );
324         slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
325         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
326         modv = slapi_x_modifications2ldapmods( &modlist );
327         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
328
329         rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
330         if ( rs->sr_err != 0 ) {
331                 /*
332                  * A preoperation plugin failure will abort the
333                  * entire operation.
334                  */
335 #ifdef NEW_LOGGING
336                 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
337                                 "failed\n", 0, 0, 0 );
338 #else
339                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
340                                 0, 0, 0);
341 #endif
342                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0) {
343                         rs->sr_err = LDAP_OTHER;
344                 }
345                 slapi_x_free_ldapmods( modv );
346                 modv = NULL;
347                 goto cleanup;
348         }
349
350         /*
351          * It's possible that the preoperation plugin changed the
352          * modification array, so we need to convert it back to
353          * a Modification list.
354          *
355          * Calling slapi_x_modifications2ldapmods() destroyed modlist so
356          * we don't need to free it.
357          */
358         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
359         modlist = slapi_x_ldapmods2modifications( modv );
360
361         /*
362          * NB: it is valid for the plugin to return no modifications
363          * (for example, a plugin might store some attributes elsewhere
364          * and remove them from the modification list; if only those
365          * attribute types were included in the modification request,
366          * then slapi_x_ldapmods2modifications() above will return
367          * NULL).
368          */
369         if ( modlist == NULL ) {
370                 rs->sr_err = LDAP_SUCCESS;
371                 send_ldap_result( op, rs );
372                 goto cleanup;
373         }
374 #endif /* defined( LDAP_SLAPI ) */
375
376         /*
377          * do the modify if 1 && (2 || 3)
378          * 1) there is a modify function implemented in this backend;
379          * 2) this backend is master for what it holds;
380          * 3) it's a replica and the dn supplied is the update_ndn.
381          */
382         if ( op->o_bd->be_modify ) {
383                 /* do the update here */
384                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
385 #ifndef SLAPD_MULTIMASTER
386                 /* Multimaster slapd does not have to check for replicator dn
387                  * because it accepts each modify request
388                  */
389                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
390 #endif
391                 {
392                         int update = op->o_bd->be_update_ndn.bv_len;
393                         char textbuf[SLAP_TEXT_BUFLEN];
394                         size_t textlen = sizeof textbuf;
395
396                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
397                                 textbuf, textlen, op->o_tmpmemctx );
398
399                         if( rs->sr_err != LDAP_SUCCESS ) {
400                                 send_ldap_result( op, rs );
401                                 goto cleanup;
402                         }
403
404                         if ( !repl_user ) {
405                                 for( modtail = &modlist;
406                                         *modtail != NULL;
407                                         modtail = &(*modtail)->sml_next )
408                                 {
409                                         /* empty */
410                                 }
411
412                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
413                                         &rs->sr_text, textbuf, textlen );
414                                 if( rs->sr_err != LDAP_SUCCESS ) {
415                                         send_ldap_result( op, rs );
416                                         goto cleanup;
417                                 }
418                         }
419
420                         op->orm_modlist = modlist;
421                         if ( (op->o_bd->be_modify)( op, rs ) == 0
422 #ifdef SLAPD_MULTIMASTER
423                                 && !repl_user
424 #endif
425                         ) {
426                                 /* but we log only the ones not from a replicator user */
427                                 replog( op );
428                         }
429
430 #ifndef SLAPD_MULTIMASTER
431                 /* send a referral */
432                 } else {
433                         BerVarray defref = op->o_bd->be_update_refs
434                                 ? op->o_bd->be_update_refs : default_referral;
435                         rs->sr_ref = referral_rewrite( defref,
436                                 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
437
438                         if (!rs->sr_ref) rs->sr_ref = defref;
439                         rs->sr_err = LDAP_REFERRAL;
440                         send_ldap_result( op, rs );
441                         if (rs->sr_ref != defref) ber_bvarray_free( rs->sr_ref );
442 #endif
443                 }
444         } else {
445                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
446                     "operation not supported within namingContext" );
447         }
448
449 #if defined( LDAP_SLAPI )
450         if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
451 #ifdef NEW_LOGGING
452                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
453                                 "failed\n", 0, 0, 0 );
454 #else
455                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
456                                 "failed.\n", 0, 0, 0);
457 #endif
458         }
459 #endif /* defined( LDAP_SLAPI ) */
460
461 cleanup:
462         free( op->o_req_dn.bv_val );
463         free( op->o_req_ndn.bv_val );
464         if ( modlist != NULL ) slap_mods_free( modlist );
465 #if defined( LDAP_SLAPI )
466         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
467 #endif
468         return rs->sr_err;
469 }
470
471 /*
472  * Do basic attribute type checking and syntax validation.
473  */
474 int slap_mods_check(
475         Modifications *ml,
476         int update,
477         const char **text,
478         char *textbuf,
479         size_t textlen,
480         void *ctx )
481 {
482         int rc;
483
484         for( ; ml != NULL; ml = ml->sml_next ) {
485                 AttributeDescription *ad = NULL;
486
487                 /* convert to attribute description */
488                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
489
490                 if( rc != LDAP_SUCCESS ) {
491                         snprintf( textbuf, textlen, "%s: %s",
492                                 ml->sml_type.bv_val, *text );
493                         *text = textbuf;
494                         return rc;
495                 }
496
497                 ad = ml->sml_desc;
498
499                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
500                         && !slap_ad_is_binary( ad ))
501                 {
502                         /* attribute requires binary transfer */
503                         snprintf( textbuf, textlen,
504                                 "%s: requires ;binary transfer",
505                                 ml->sml_type.bv_val );
506                         *text = textbuf;
507                         return LDAP_UNDEFINED_TYPE;
508                 }
509
510                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
511                         && slap_ad_is_binary( ad ))
512                 {
513                         /* attribute requires binary transfer */
514                         snprintf( textbuf, textlen,
515                                 "%s: disallows ;binary transfer",
516                                 ml->sml_type.bv_val );
517                         *text = textbuf;
518                         return LDAP_UNDEFINED_TYPE;
519                 }
520
521                 if( slap_ad_is_tag_range( ad )) {
522                         /* attribute requires binary transfer */
523                         snprintf( textbuf, textlen,
524                                 "%s: inappropriate use of tag range option",
525                                 ml->sml_type.bv_val );
526                         *text = textbuf;
527                         return LDAP_UNDEFINED_TYPE;
528                 }
529
530                 if (!update && is_at_no_user_mod( ad->ad_type )) {
531                         /* user modification disallowed */
532                         snprintf( textbuf, textlen,
533                                 "%s: no user modification allowed",
534                                 ml->sml_type.bv_val );
535                         *text = textbuf;
536                         return LDAP_CONSTRAINT_VIOLATION;
537                 }
538
539                 if ( is_at_obsolete( ad->ad_type ) &&
540                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
541                 {
542                         /*
543                          * attribute is obsolete,
544                          * only allow replace/delete with no values
545                          */
546                         snprintf( textbuf, textlen,
547                                 "%s: attribute is obsolete",
548                                 ml->sml_type.bv_val );
549                         *text = textbuf;
550                         return LDAP_CONSTRAINT_VIOLATION;
551                 }
552
553                 /*
554                  * check values
555                  */
556                 if( ml->sml_values != NULL ) {
557                         ber_len_t nvals;
558                         slap_syntax_validate_func *validate =
559                                 ad->ad_type->sat_syntax->ssyn_validate;
560                         slap_syntax_transform_func *pretty =
561                                 ad->ad_type->sat_syntax->ssyn_pretty;
562  
563                         if( !pretty && !validate ) {
564                                 *text = "no validator for syntax";
565                                 snprintf( textbuf, textlen,
566                                         "%s: no validator for syntax %s",
567                                         ml->sml_type.bv_val,
568                                         ad->ad_type->sat_syntax->ssyn_oid );
569                                 *text = textbuf;
570                                 return LDAP_INVALID_SYNTAX;
571                         }
572
573                         /*
574                          * check that each value is valid per syntax
575                          *      and pretty if appropriate
576                          */
577                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
578                                 struct berval pval;
579                                 if( pretty ) {
580                                         rc = pretty( ad->ad_type->sat_syntax,
581                                                 &ml->sml_values[nvals], &pval, ctx );
582                                 } else {
583                                         rc = validate( ad->ad_type->sat_syntax,
584                                                 &ml->sml_values[nvals] );
585                                 }
586
587                                 if( rc != 0 ) {
588                                         snprintf( textbuf, textlen,
589                                                 "%s: value #%ld invalid per syntax",
590                                                 ml->sml_type.bv_val, (long) nvals );
591                                         *text = textbuf;
592                                         return LDAP_INVALID_SYNTAX;
593                                 }
594
595                                 if( pretty ) {
596                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
597                                         ml->sml_values[nvals] = pval;
598                                 }
599                         }
600
601                         /*
602                          * a rough single value check... an additional check is needed
603                          * to catch add of single value to existing single valued attribute
604                          */
605                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
606                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
607                         {
608                                 snprintf( textbuf, textlen,
609                                         "%s: multiple values provided",
610                                         ml->sml_type.bv_val );
611                                 *text = textbuf;
612                                 return LDAP_CONSTRAINT_VIOLATION;
613                         }
614
615                         if( nvals && ad->ad_type->sat_equality &&
616                                 ad->ad_type->sat_equality->smr_normalize )
617                         {
618                                 ml->sml_nvalues = ber_memalloc_x( (nvals+1)*sizeof(struct berval), ctx );
619                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
620                                         rc = ad->ad_type->sat_equality->smr_normalize(
621                                                 0,
622                                                 ad->ad_type->sat_syntax,
623                                                 ad->ad_type->sat_equality,
624                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
625                                         if( rc ) {
626 #ifdef NEW_LOGGING
627                                                 LDAP_LOG( OPERATION, DETAIL1,
628                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
629                                                         rc, 0, 0 );
630 #else
631                                                 Debug( LDAP_DEBUG_ANY,
632                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
633                                                         rc, 0, 0 );
634 #endif
635                                                 snprintf( textbuf, textlen,
636                                                         "%s: value #%ld normalization failed",
637                                                         ml->sml_type.bv_val, (long) nvals );
638                                                 *text = textbuf;
639                                                 return rc;
640                                         }
641                                 }
642                                 ml->sml_nvalues[nvals].bv_val = NULL;
643                                 ml->sml_nvalues[nvals].bv_len = 0;
644                         }
645                 }
646         }
647
648         return LDAP_SUCCESS;
649 }
650
651 int slap_mods_opattrs(
652         Operation *op,
653         Modifications *mods,
654         Modifications **modtail,
655         const char **text,
656         char *textbuf, size_t textlen )
657 {
658         struct berval name, timestamp, csn;
659         struct berval nname;
660         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
661         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
662         Modifications *mod;
663
664         int mop = op->o_tag == LDAP_REQ_ADD
665                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
666
667         assert( modtail != NULL );
668         assert( *modtail == NULL );
669
670         if( SLAP_LASTMOD(op->o_bd) ) {
671                 struct tm *ltm;
672                 time_t now = slap_get_time();
673
674                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
675                 ltm = gmtime( &now );
676                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
677
678                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
679                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
680                 csn.bv_val = csnbuf;
681
682                 timestamp.bv_val = timebuf;
683                 timestamp.bv_len = strlen(timebuf);
684
685                 if( op->o_dn.bv_len == 0 ) {
686                         name.bv_val = SLAPD_ANONYMOUS;
687                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
688                         nname = name;
689                 } else {
690                         name = op->o_dn;
691                         nname = op->o_ndn;
692                 }
693         }
694
695         if( op->o_tag == LDAP_REQ_ADD ) {
696                 struct berval tmpval;
697
698                 if( global_schemacheck ) {
699                         int rc = mods_structural_class( mods, &tmpval,
700                                 text, textbuf, textlen );
701                         if( rc != LDAP_SUCCESS ) {
702                                 return rc;
703                         }
704
705                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
706                         mod->sml_op = mop;
707                         mod->sml_type.bv_val = NULL;
708                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
709                         mod->sml_values =
710                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
711                         ber_dupbv( &mod->sml_values[0], &tmpval );
712                         mod->sml_values[1].bv_len = 0;
713                         mod->sml_values[1].bv_val = NULL;
714                         assert( mod->sml_values[0].bv_val );
715                         mod->sml_nvalues =
716                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
717                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
718                         mod->sml_nvalues[1].bv_len = 0;
719                         mod->sml_nvalues[1].bv_val = NULL;
720                         assert( mod->sml_nvalues[0].bv_val );
721                         *modtail = mod;
722                         modtail = &mod->sml_next;
723                 }
724
725                 if( SLAP_LASTMOD(op->o_bd) ) {
726                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
727
728                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
729                         tmpval.bv_val = uuidbuf;
730                 
731                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
732                         mod->sml_op = mop;
733                         mod->sml_type.bv_val = NULL;
734                         mod->sml_desc = slap_schema.si_ad_entryUUID;
735                         mod->sml_values =
736                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
737                         ber_dupbv( &mod->sml_values[0], &tmpval );
738                         mod->sml_values[1].bv_len = 0;
739                         mod->sml_values[1].bv_val = NULL;
740                         assert( mod->sml_values[0].bv_val );
741                         mod->sml_nvalues = NULL;
742                         *modtail = mod;
743                         modtail = &mod->sml_next;
744
745                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
746                         mod->sml_op = mop;
747                         mod->sml_type.bv_val = NULL;
748                         mod->sml_desc = slap_schema.si_ad_creatorsName;
749                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
750                         ber_dupbv( &mod->sml_values[0], &name );
751                         mod->sml_values[1].bv_len = 0;
752                         mod->sml_values[1].bv_val = NULL;
753                         assert( mod->sml_values[0].bv_val );
754                         mod->sml_nvalues =
755                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
756                         ber_dupbv( &mod->sml_nvalues[0], &nname );
757                         mod->sml_nvalues[1].bv_len = 0;
758                         mod->sml_nvalues[1].bv_val = NULL;
759                         assert( mod->sml_nvalues[0].bv_val );
760                         *modtail = mod;
761                         modtail = &mod->sml_next;
762
763                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
764                         mod->sml_op = mop;
765                         mod->sml_type.bv_val = NULL;
766                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
767                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
768                         ber_dupbv( &mod->sml_values[0], &timestamp );
769                         mod->sml_values[1].bv_len = 0;
770                         mod->sml_values[1].bv_val = NULL;
771                         assert( mod->sml_values[0].bv_val );
772                         mod->sml_nvalues = NULL;
773                         *modtail = mod;
774                         modtail = &mod->sml_next;
775                 }
776         }
777
778         if( SLAP_LASTMOD(op->o_bd) ) {
779                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
780                 mod->sml_op = mop;
781                 mod->sml_type.bv_val = NULL;
782                 mod->sml_desc = slap_schema.si_ad_entryCSN;
783                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
784                 ber_dupbv( &mod->sml_values[0], &csn );
785                 mod->sml_values[1].bv_len = 0;
786                 mod->sml_values[1].bv_val = NULL;
787                 assert( mod->sml_values[0].bv_val );
788                 mod->sml_nvalues = NULL;
789                 *modtail = mod;
790                 modtail = &mod->sml_next;
791
792                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
793                 mod->sml_op = mop;
794                 mod->sml_type.bv_val = NULL;
795                 mod->sml_desc = slap_schema.si_ad_modifiersName;
796                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
797                 ber_dupbv( &mod->sml_values[0], &name );
798                 mod->sml_values[1].bv_len = 0;
799                 mod->sml_values[1].bv_val = NULL;
800                 assert( mod->sml_values[0].bv_val );
801                 mod->sml_nvalues =
802                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
803                 ber_dupbv( &mod->sml_nvalues[0], &nname );
804                 mod->sml_nvalues[1].bv_len = 0;
805                 mod->sml_nvalues[1].bv_val = NULL;
806                 assert( mod->sml_nvalues[0].bv_val );
807                 *modtail = mod;
808                 modtail = &mod->sml_next;
809
810                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
811                 mod->sml_op = mop;
812                 mod->sml_type.bv_val = NULL;
813                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
814                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
815                 ber_dupbv( &mod->sml_values[0], &timestamp );
816                 mod->sml_values[1].bv_len = 0;
817                 mod->sml_values[1].bv_val = NULL;
818                 assert( mod->sml_values[0].bv_val );
819                 mod->sml_nvalues = NULL;
820                 *modtail = mod;
821                 modtail = &mod->sml_next;
822         }
823
824         *modtail = NULL;
825         return LDAP_SUCCESS;
826 }
827