]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
8944b2297f27f9741cf1e54d0c87fee43a7dc742
[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 );
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 );
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 {
481         int rc;
482
483         for( ; ml != NULL; ml = ml->sml_next ) {
484                 AttributeDescription *ad = NULL;
485
486                 /* convert to attribute description */
487                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
488
489                 if( rc != LDAP_SUCCESS ) {
490                         snprintf( textbuf, textlen, "%s: %s",
491                                 ml->sml_type.bv_val, *text );
492                         *text = textbuf;
493                         return rc;
494                 }
495
496                 ad = ml->sml_desc;
497
498                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
499                         && !slap_ad_is_binary( ad ))
500                 {
501                         /* attribute requires binary transfer */
502                         snprintf( textbuf, textlen,
503                                 "%s: requires ;binary transfer",
504                                 ml->sml_type.bv_val );
505                         *text = textbuf;
506                         return LDAP_UNDEFINED_TYPE;
507                 }
508
509                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
510                         && slap_ad_is_binary( ad ))
511                 {
512                         /* attribute requires binary transfer */
513                         snprintf( textbuf, textlen,
514                                 "%s: disallows ;binary transfer",
515                                 ml->sml_type.bv_val );
516                         *text = textbuf;
517                         return LDAP_UNDEFINED_TYPE;
518                 }
519
520                 if( slap_ad_is_tag_range( ad )) {
521                         /* attribute requires binary transfer */
522                         snprintf( textbuf, textlen,
523                                 "%s: inappropriate use of tag range option",
524                                 ml->sml_type.bv_val );
525                         *text = textbuf;
526                         return LDAP_UNDEFINED_TYPE;
527                 }
528
529                 if (!update && is_at_no_user_mod( ad->ad_type )) {
530                         /* user modification disallowed */
531                         snprintf( textbuf, textlen,
532                                 "%s: no user modification allowed",
533                                 ml->sml_type.bv_val );
534                         *text = textbuf;
535                         return LDAP_CONSTRAINT_VIOLATION;
536                 }
537
538                 if ( is_at_obsolete( ad->ad_type ) &&
539                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
540                 {
541                         /*
542                          * attribute is obsolete,
543                          * only allow replace/delete with no values
544                          */
545                         snprintf( textbuf, textlen,
546                                 "%s: attribute is obsolete",
547                                 ml->sml_type.bv_val );
548                         *text = textbuf;
549                         return LDAP_CONSTRAINT_VIOLATION;
550                 }
551
552                 /*
553                  * check values
554                  */
555                 if( ml->sml_values != NULL ) {
556                         ber_len_t nvals;
557                         slap_syntax_validate_func *validate =
558                                 ad->ad_type->sat_syntax->ssyn_validate;
559                         slap_syntax_transform_func *pretty =
560                                 ad->ad_type->sat_syntax->ssyn_pretty;
561  
562                         if( !pretty && !validate ) {
563                                 *text = "no validator for syntax";
564                                 snprintf( textbuf, textlen,
565                                         "%s: no validator for syntax %s",
566                                         ml->sml_type.bv_val,
567                                         ad->ad_type->sat_syntax->ssyn_oid );
568                                 *text = textbuf;
569                                 return LDAP_INVALID_SYNTAX;
570                         }
571
572                         /*
573                          * check that each value is valid per syntax
574                          *      and pretty if appropriate
575                          */
576                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
577                                 struct berval pval;
578                                 if( pretty ) {
579                                         rc = pretty( ad->ad_type->sat_syntax,
580                                                 &ml->sml_values[nvals], &pval );
581                                 } else {
582                                         rc = validate( ad->ad_type->sat_syntax,
583                                                 &ml->sml_values[nvals] );
584                                 }
585
586                                 if( rc != 0 ) {
587                                         snprintf( textbuf, textlen,
588                                                 "%s: value #%ld invalid per syntax",
589                                                 ml->sml_type.bv_val, (long) nvals );
590                                         *text = textbuf;
591                                         return LDAP_INVALID_SYNTAX;
592                                 }
593
594                                 if( pretty ) {
595                                         ber_memfree( ml->sml_values[nvals].bv_val );
596                                         ml->sml_values[nvals] = pval;
597                                 }
598                         }
599
600                         /*
601                          * a rough single value check... an additional check is needed
602                          * to catch add of single value to existing single valued attribute
603                          */
604                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
605                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
606                         {
607                                 snprintf( textbuf, textlen,
608                                         "%s: multiple values provided",
609                                         ml->sml_type.bv_val );
610                                 *text = textbuf;
611                                 return LDAP_CONSTRAINT_VIOLATION;
612                         }
613
614                         if( nvals && ad->ad_type->sat_equality &&
615                                 ad->ad_type->sat_equality->smr_normalize )
616                         {
617                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
618                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
619                                         rc = ad->ad_type->sat_equality->smr_normalize(
620                                                 0,
621                                                 ad->ad_type->sat_syntax,
622                                                 ad->ad_type->sat_equality,
623                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals] );
624                                         if( rc ) {
625 #ifdef NEW_LOGGING
626                                                 LDAP_LOG( OPERATION, DETAIL1,
627                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
628                                                         rc, 0, 0 );
629 #else
630                                                 Debug( LDAP_DEBUG_ANY,
631                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
632                                                         rc, 0, 0 );
633 #endif
634                                                 snprintf( textbuf, textlen,
635                                                         "%s: value #%ld normalization failed",
636                                                         ml->sml_type.bv_val, (long) nvals );
637                                                 *text = textbuf;
638                                                 return rc;
639                                         }
640                                 }
641                                 ml->sml_nvalues[nvals].bv_val = NULL;
642                                 ml->sml_nvalues[nvals].bv_len = 0;
643                         }
644                 }
645         }
646
647         return LDAP_SUCCESS;
648 }
649
650 int slap_mods_opattrs(
651         Operation *op,
652         Modifications *mods,
653         Modifications **modtail,
654         const char **text,
655         char *textbuf, size_t textlen )
656 {
657         struct berval name, timestamp, csn;
658         struct berval nname;
659         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
660         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
661         Modifications *mod;
662
663         int mop = op->o_tag == LDAP_REQ_ADD
664                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
665
666         assert( modtail != NULL );
667         assert( *modtail == NULL );
668
669         if( SLAP_LASTMOD(op->o_bd) ) {
670                 struct tm *ltm;
671                 time_t now = slap_get_time();
672
673                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
674                 ltm = gmtime( &now );
675                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
676
677                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
678                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
679                 csn.bv_val = csnbuf;
680
681                 timestamp.bv_val = timebuf;
682                 timestamp.bv_len = strlen(timebuf);
683
684                 if( op->o_dn.bv_len == 0 ) {
685                         name.bv_val = SLAPD_ANONYMOUS;
686                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
687                         nname = name;
688                 } else {
689                         name = op->o_dn;
690                         nname = op->o_ndn;
691                 }
692         }
693
694         if( op->o_tag == LDAP_REQ_ADD ) {
695                 struct berval tmpval;
696
697                 if( global_schemacheck ) {
698                         int rc = mods_structural_class( mods, &tmpval,
699                                 text, textbuf, textlen );
700                         if( rc != LDAP_SUCCESS ) {
701                                 return rc;
702                         }
703
704                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
705                         mod->sml_op = mop;
706                         mod->sml_type.bv_val = NULL;
707                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
708                         mod->sml_values =
709                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
710                         ber_dupbv( &mod->sml_values[0], &tmpval );
711                         mod->sml_values[1].bv_len = 0;
712                         mod->sml_values[1].bv_val = NULL;
713                         assert( mod->sml_values[0].bv_val );
714                         mod->sml_nvalues =
715                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
716                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
717                         mod->sml_nvalues[1].bv_len = 0;
718                         mod->sml_nvalues[1].bv_val = NULL;
719                         assert( mod->sml_nvalues[0].bv_val );
720                         *modtail = mod;
721                         modtail = &mod->sml_next;
722                 }
723
724                 if( SLAP_LASTMOD(op->o_bd) ) {
725                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
726
727                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
728                         tmpval.bv_val = uuidbuf;
729                 
730                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
731                         mod->sml_op = mop;
732                         mod->sml_type.bv_val = NULL;
733                         mod->sml_desc = slap_schema.si_ad_entryUUID;
734                         mod->sml_values =
735                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
736                         ber_dupbv( &mod->sml_values[0], &tmpval );
737                         mod->sml_values[1].bv_len = 0;
738                         mod->sml_values[1].bv_val = NULL;
739                         assert( mod->sml_values[0].bv_val );
740                         mod->sml_nvalues = NULL;
741                         *modtail = mod;
742                         modtail = &mod->sml_next;
743
744                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
745                         mod->sml_op = mop;
746                         mod->sml_type.bv_val = NULL;
747                         mod->sml_desc = slap_schema.si_ad_creatorsName;
748                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
749                         ber_dupbv( &mod->sml_values[0], &name );
750                         mod->sml_values[1].bv_len = 0;
751                         mod->sml_values[1].bv_val = NULL;
752                         assert( mod->sml_values[0].bv_val );
753                         mod->sml_nvalues =
754                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
755                         ber_dupbv( &mod->sml_nvalues[0], &nname );
756                         mod->sml_nvalues[1].bv_len = 0;
757                         mod->sml_nvalues[1].bv_val = NULL;
758                         assert( mod->sml_nvalues[0].bv_val );
759                         *modtail = mod;
760                         modtail = &mod->sml_next;
761
762                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
763                         mod->sml_op = mop;
764                         mod->sml_type.bv_val = NULL;
765                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
766                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
767                         ber_dupbv( &mod->sml_values[0], &timestamp );
768                         mod->sml_values[1].bv_len = 0;
769                         mod->sml_values[1].bv_val = NULL;
770                         assert( mod->sml_values[0].bv_val );
771                         mod->sml_nvalues = NULL;
772                         *modtail = mod;
773                         modtail = &mod->sml_next;
774                 }
775         }
776
777         if( SLAP_LASTMOD(op->o_bd) ) {
778                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
779                 mod->sml_op = mop;
780                 mod->sml_type.bv_val = NULL;
781                 mod->sml_desc = slap_schema.si_ad_entryCSN;
782                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
783                 ber_dupbv( &mod->sml_values[0], &csn );
784                 mod->sml_values[1].bv_len = 0;
785                 mod->sml_values[1].bv_val = NULL;
786                 assert( mod->sml_values[0].bv_val );
787                 mod->sml_nvalues = NULL;
788                 *modtail = mod;
789                 modtail = &mod->sml_next;
790
791                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
792                 mod->sml_op = mop;
793                 mod->sml_type.bv_val = NULL;
794                 mod->sml_desc = slap_schema.si_ad_modifiersName;
795                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
796                 ber_dupbv( &mod->sml_values[0], &name );
797                 mod->sml_values[1].bv_len = 0;
798                 mod->sml_values[1].bv_val = NULL;
799                 assert( mod->sml_values[0].bv_val );
800                 mod->sml_nvalues =
801                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
802                 ber_dupbv( &mod->sml_nvalues[0], &nname );
803                 mod->sml_nvalues[1].bv_len = 0;
804                 mod->sml_nvalues[1].bv_val = NULL;
805                 assert( mod->sml_nvalues[0].bv_val );
806                 *modtail = mod;
807                 modtail = &mod->sml_next;
808
809                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
810                 mod->sml_op = mop;
811                 mod->sml_type.bv_val = NULL;
812                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
813                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
814                 ber_dupbv( &mod->sml_values[0], &timestamp );
815                 mod->sml_values[1].bv_len = 0;
816                 mod->sml_values[1].bv_val = NULL;
817                 assert( mod->sml_values[0].bv_val );
818                 mod->sml_nvalues = NULL;
819                 *modtail = mod;
820                 modtail = &mod->sml_next;
821         }
822
823         *modtail = NULL;
824         return LDAP_SUCCESS;
825 }
826