]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
a04d49a61c1708f623768b596cedc9283bd18c73
[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                 if (rs->sr_ref != NULL ) {
305                         rs->sr_err = LDAP_REFERRAL;
306                         send_ldap_result( op, rs );
307
308                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
309                 } else {
310                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
311                                         "referral missing" );
312                 }
313                 goto cleanup;
314         }
315
316         /* check restrictions */
317         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
318                 send_ldap_result( op, rs );
319                 goto cleanup;
320         }
321
322         /* check for referrals */
323         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
324                 goto cleanup;
325         }
326
327 #if defined( LDAP_SLAPI )
328         slapi_x_pblock_set_operation( pb, op );
329         slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
330         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
331         modv = slapi_x_modifications2ldapmods( &modlist );
332         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
333
334         rs->sr_err = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
335         if ( rs->sr_err < 0 ) {
336                 /*
337                  * A preoperation plugin failure will abort the
338                  * entire operation.
339                  */
340 #ifdef NEW_LOGGING
341                 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
342                                 "failed\n", 0, 0, 0 );
343 #else
344                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
345                                 0, 0, 0);
346 #endif
347                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rs->sr_err ) != 0) {
348                         rs->sr_err = LDAP_OTHER;
349                 }
350                 slapi_x_free_ldapmods( modv );
351                 modv = NULL;
352                 goto cleanup;
353         }
354
355         /*
356          * It's possible that the preoperation plugin changed the
357          * modification array, so we need to convert it back to
358          * a Modification list.
359          *
360          * Calling slapi_x_modifications2ldapmods() destroyed modlist so
361          * we don't need to free it.
362          */
363         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
364         modlist = slapi_x_ldapmods2modifications( modv );
365
366         /*
367          * NB: it is valid for the plugin to return no modifications
368          * (for example, a plugin might store some attributes elsewhere
369          * and remove them from the modification list; if only those
370          * attribute types were included in the modification request,
371          * then slapi_x_ldapmods2modifications() above will return
372          * NULL).
373          */
374         if ( modlist == NULL ) {
375                 rs->sr_err = LDAP_SUCCESS;
376                 send_ldap_result( op, rs );
377                 goto cleanup;
378         }
379 #endif /* defined( LDAP_SLAPI ) */
380
381         /*
382          * do the modify if 1 && (2 || 3)
383          * 1) there is a modify function implemented in this backend;
384          * 2) this backend is master for what it holds;
385          * 3) it's a replica and the dn supplied is the update_ndn.
386          */
387         if ( op->o_bd->be_modify ) {
388                 /* do the update here */
389                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
390 #ifndef SLAPD_MULTIMASTER
391                 /* Multimaster slapd does not have to check for replicator dn
392                  * because it accepts each modify request
393                  */
394                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
395 #endif
396                 {
397                         int update = op->o_bd->be_update_ndn.bv_len;
398                         char textbuf[SLAP_TEXT_BUFLEN];
399                         size_t textlen = sizeof textbuf;
400
401                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
402                                 textbuf, textlen, NULL );
403
404                         if( rs->sr_err != LDAP_SUCCESS ) {
405                                 send_ldap_result( op, rs );
406                                 goto cleanup;
407                         }
408
409                         if ( !repl_user ) {
410                                 for( modtail = &modlist;
411                                         *modtail != NULL;
412                                         modtail = &(*modtail)->sml_next )
413                                 {
414                                         /* empty */
415                                 }
416
417                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
418                                         &rs->sr_text, textbuf, textlen );
419                                 if( rs->sr_err != LDAP_SUCCESS ) {
420                                         send_ldap_result( op, rs );
421                                         goto cleanup;
422                                 }
423                         }
424
425                         op->orm_modlist = modlist;
426                         if ( (op->o_bd->be_modify)( op, rs ) == 0
427 #ifdef SLAPD_MULTIMASTER
428                                 && !repl_user
429 #endif
430                         ) {
431                                 /* but we log only the ones not from a replicator user */
432                                 replog( op );
433                         }
434
435 #ifndef SLAPD_MULTIMASTER
436                 /* send a referral */
437                 } else {
438                         BerVarray defref = op->o_bd->be_update_refs
439                                 ? op->o_bd->be_update_refs : default_referral;
440                         if ( defref != NULL ) {
441                                 rs->sr_ref = referral_rewrite( defref,
442                                         NULL, &op->o_req_dn,
443                                         LDAP_SCOPE_DEFAULT );
444                                 if (!rs->sr_ref) rs->sr_ref = defref;
445                                 rs->sr_err = LDAP_REFERRAL;
446                                 send_ldap_result( op, rs );
447                                 if (rs->sr_ref != defref) {
448                                         ber_bvarray_free( rs->sr_ref );
449                                 }
450                         } else {
451                                 send_ldap_error( op, rs,
452                                                 LDAP_UNWILLING_TO_PERFORM,
453                                                 "referral missing" );
454                         }
455 #endif
456                 }
457         } else {
458                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
459                     "operation not supported within namingContext" );
460         }
461
462 #if defined( LDAP_SLAPI )
463         if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 ) {
464 #ifdef NEW_LOGGING
465                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
466                                 "failed\n", 0, 0, 0 );
467 #else
468                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
469                                 "failed.\n", 0, 0, 0);
470 #endif
471         }
472 #endif /* defined( LDAP_SLAPI ) */
473
474 cleanup:
475         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
476         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
477         if ( modlist != NULL ) slap_mods_free( modlist );
478 #if defined( LDAP_SLAPI )
479         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
480 #endif
481         return rs->sr_err;
482 }
483
484 /*
485  * Do basic attribute type checking and syntax validation.
486  */
487 int slap_mods_check(
488         Modifications *ml,
489         int update,
490         const char **text,
491         char *textbuf,
492         size_t textlen,
493         void *ctx )
494 {
495         int rc;
496
497         for( ; ml != NULL; ml = ml->sml_next ) {
498                 AttributeDescription *ad = NULL;
499
500                 /* convert to attribute description */
501                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
502
503                 if( rc != LDAP_SUCCESS ) {
504                         snprintf( textbuf, textlen, "%s: %s",
505                                 ml->sml_type.bv_val, *text );
506                         *text = textbuf;
507                         return rc;
508                 }
509
510                 ad = ml->sml_desc;
511
512                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
513                         && !slap_ad_is_binary( ad ))
514                 {
515                         /* attribute requires binary transfer */
516                         snprintf( textbuf, textlen,
517                                 "%s: requires ;binary transfer",
518                                 ml->sml_type.bv_val );
519                         *text = textbuf;
520                         return LDAP_UNDEFINED_TYPE;
521                 }
522
523                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
524                         && slap_ad_is_binary( ad ))
525                 {
526                         /* attribute requires binary transfer */
527                         snprintf( textbuf, textlen,
528                                 "%s: disallows ;binary transfer",
529                                 ml->sml_type.bv_val );
530                         *text = textbuf;
531                         return LDAP_UNDEFINED_TYPE;
532                 }
533
534                 if( slap_ad_is_tag_range( ad )) {
535                         /* attribute requires binary transfer */
536                         snprintf( textbuf, textlen,
537                                 "%s: inappropriate use of tag range option",
538                                 ml->sml_type.bv_val );
539                         *text = textbuf;
540                         return LDAP_UNDEFINED_TYPE;
541                 }
542
543                 if (!update && is_at_no_user_mod( ad->ad_type )) {
544                         /* user modification disallowed */
545                         snprintf( textbuf, textlen,
546                                 "%s: no user modification allowed",
547                                 ml->sml_type.bv_val );
548                         *text = textbuf;
549                         return LDAP_CONSTRAINT_VIOLATION;
550                 }
551
552                 if ( is_at_obsolete( ad->ad_type ) &&
553                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
554                 {
555                         /*
556                          * attribute is obsolete,
557                          * only allow replace/delete with no values
558                          */
559                         snprintf( textbuf, textlen,
560                                 "%s: attribute is obsolete",
561                                 ml->sml_type.bv_val );
562                         *text = textbuf;
563                         return LDAP_CONSTRAINT_VIOLATION;
564                 }
565
566                 /*
567                  * check values
568                  */
569                 if( ml->sml_values != NULL ) {
570                         ber_len_t nvals;
571                         slap_syntax_validate_func *validate =
572                                 ad->ad_type->sat_syntax->ssyn_validate;
573                         slap_syntax_transform_func *pretty =
574                                 ad->ad_type->sat_syntax->ssyn_pretty;
575  
576                         if( !pretty && !validate ) {
577                                 *text = "no validator for syntax";
578                                 snprintf( textbuf, textlen,
579                                         "%s: no validator for syntax %s",
580                                         ml->sml_type.bv_val,
581                                         ad->ad_type->sat_syntax->ssyn_oid );
582                                 *text = textbuf;
583                                 return LDAP_INVALID_SYNTAX;
584                         }
585
586                         /*
587                          * check that each value is valid per syntax
588                          *      and pretty if appropriate
589                          */
590                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
591                                 struct berval pval;
592                                 if( pretty ) {
593                                         rc = pretty( ad->ad_type->sat_syntax,
594                                                 &ml->sml_values[nvals], &pval, ctx );
595                                 } else {
596                                         rc = validate( ad->ad_type->sat_syntax,
597                                                 &ml->sml_values[nvals] );
598                                 }
599
600                                 if( rc != 0 ) {
601                                         snprintf( textbuf, textlen,
602                                                 "%s: value #%ld invalid per syntax",
603                                                 ml->sml_type.bv_val, (long) nvals );
604                                         *text = textbuf;
605                                         return LDAP_INVALID_SYNTAX;
606                                 }
607
608                                 if( pretty ) {
609                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
610                                         ml->sml_values[nvals] = pval;
611                                 }
612                         }
613
614                         /*
615                          * a rough single value check... an additional check is needed
616                          * to catch add of single value to existing single valued attribute
617                          */
618                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
619                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
620                         {
621                                 snprintf( textbuf, textlen,
622                                         "%s: multiple values provided",
623                                         ml->sml_type.bv_val );
624                                 *text = textbuf;
625                                 return LDAP_CONSTRAINT_VIOLATION;
626                         }
627
628                         if( nvals && ad->ad_type->sat_equality &&
629                                 ad->ad_type->sat_equality->smr_normalize )
630                         {
631                                 ml->sml_nvalues = ber_memalloc_x( (nvals+1)*sizeof(struct berval), ctx );
632                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
633                                         rc = ad->ad_type->sat_equality->smr_normalize(
634                                                 0,
635                                                 ad->ad_type->sat_syntax,
636                                                 ad->ad_type->sat_equality,
637                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
638                                         if( rc ) {
639 #ifdef NEW_LOGGING
640                                                 LDAP_LOG( OPERATION, DETAIL1,
641                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
642                                                         rc, 0, 0 );
643 #else
644                                                 Debug( LDAP_DEBUG_ANY,
645                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
646                                                         rc, 0, 0 );
647 #endif
648                                                 snprintf( textbuf, textlen,
649                                                         "%s: value #%ld normalization failed",
650                                                         ml->sml_type.bv_val, (long) nvals );
651                                                 *text = textbuf;
652                                                 return rc;
653                                         }
654                                 }
655                                 ml->sml_nvalues[nvals].bv_val = NULL;
656                                 ml->sml_nvalues[nvals].bv_len = 0;
657                         }
658                 }
659         }
660
661         return LDAP_SUCCESS;
662 }
663
664 int slap_mods_opattrs(
665         Operation *op,
666         Modifications *mods,
667         Modifications **modtail,
668         const char **text,
669         char *textbuf, size_t textlen )
670 {
671         struct berval name, timestamp, csn;
672         struct berval nname;
673         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
674         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
675         Modifications *mod;
676
677         int mop = op->o_tag == LDAP_REQ_ADD
678                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
679
680         assert( modtail != NULL );
681         assert( *modtail == NULL );
682
683         if( SLAP_LASTMOD(op->o_bd) ) {
684                 struct tm *ltm;
685                 time_t now = slap_get_time();
686
687                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
688                 ltm = gmtime( &now );
689                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
690
691                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
692                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
693                 csn.bv_val = csnbuf;
694
695                 timestamp.bv_val = timebuf;
696                 timestamp.bv_len = strlen(timebuf);
697
698                 if( op->o_dn.bv_len == 0 ) {
699                         name.bv_val = SLAPD_ANONYMOUS;
700                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
701                         nname = name;
702                 } else {
703                         name = op->o_dn;
704                         nname = op->o_ndn;
705                 }
706         }
707
708         if( op->o_tag == LDAP_REQ_ADD ) {
709                 struct berval tmpval;
710
711                 if( global_schemacheck ) {
712                         int rc = mods_structural_class( mods, &tmpval,
713                                 text, textbuf, textlen );
714                         if( rc != LDAP_SUCCESS ) {
715                                 return rc;
716                         }
717
718                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
719                         mod->sml_op = mop;
720                         mod->sml_type.bv_val = NULL;
721                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
722                         mod->sml_values =
723                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
724                         ber_dupbv( &mod->sml_values[0], &tmpval );
725                         mod->sml_values[1].bv_len = 0;
726                         mod->sml_values[1].bv_val = NULL;
727                         assert( mod->sml_values[0].bv_val );
728                         mod->sml_nvalues =
729                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
730                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
731                         mod->sml_nvalues[1].bv_len = 0;
732                         mod->sml_nvalues[1].bv_val = NULL;
733                         assert( mod->sml_nvalues[0].bv_val );
734                         *modtail = mod;
735                         modtail = &mod->sml_next;
736                 }
737
738                 if( SLAP_LASTMOD(op->o_bd) ) {
739                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
740
741                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
742                         tmpval.bv_val = uuidbuf;
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_entryUUID;
748                         mod->sml_values =
749                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
750                         ber_dupbv( &mod->sml_values[0], &tmpval );
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 = NULL;
755                         *modtail = mod;
756                         modtail = &mod->sml_next;
757
758                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
759                         mod->sml_op = mop;
760                         mod->sml_type.bv_val = NULL;
761                         mod->sml_desc = slap_schema.si_ad_creatorsName;
762                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
763                         ber_dupbv( &mod->sml_values[0], &name );
764                         mod->sml_values[1].bv_len = 0;
765                         mod->sml_values[1].bv_val = NULL;
766                         assert( mod->sml_values[0].bv_val );
767                         mod->sml_nvalues =
768                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
769                         ber_dupbv( &mod->sml_nvalues[0], &nname );
770                         mod->sml_nvalues[1].bv_len = 0;
771                         mod->sml_nvalues[1].bv_val = NULL;
772                         assert( mod->sml_nvalues[0].bv_val );
773                         *modtail = mod;
774                         modtail = &mod->sml_next;
775
776                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
777                         mod->sml_op = mop;
778                         mod->sml_type.bv_val = NULL;
779                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
780                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
781                         ber_dupbv( &mod->sml_values[0], &timestamp );
782                         mod->sml_values[1].bv_len = 0;
783                         mod->sml_values[1].bv_val = NULL;
784                         assert( mod->sml_values[0].bv_val );
785                         mod->sml_nvalues = NULL;
786                         *modtail = mod;
787                         modtail = &mod->sml_next;
788                 }
789         }
790
791         if( SLAP_LASTMOD(op->o_bd) ) {
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_entryCSN;
796                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
797                 ber_dupbv( &mod->sml_values[0], &csn );
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 = NULL;
802                 *modtail = mod;
803                 modtail = &mod->sml_next;
804
805                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
806                 mod->sml_op = mop;
807                 mod->sml_type.bv_val = NULL;
808                 mod->sml_desc = slap_schema.si_ad_modifiersName;
809                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
810                 ber_dupbv( &mod->sml_values[0], &name );
811                 mod->sml_values[1].bv_len = 0;
812                 mod->sml_values[1].bv_val = NULL;
813                 assert( mod->sml_values[0].bv_val );
814                 mod->sml_nvalues =
815                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
816                 ber_dupbv( &mod->sml_nvalues[0], &nname );
817                 mod->sml_nvalues[1].bv_len = 0;
818                 mod->sml_nvalues[1].bv_val = NULL;
819                 assert( mod->sml_nvalues[0].bv_val );
820                 *modtail = mod;
821                 modtail = &mod->sml_next;
822
823                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
824                 mod->sml_op = mop;
825                 mod->sml_type.bv_val = NULL;
826                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
827                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
828                 ber_dupbv( &mod->sml_values[0], &timestamp );
829                 mod->sml_values[1].bv_len = 0;
830                 mod->sml_values[1].bv_val = NULL;
831                 assert( mod->sml_values[0].bv_val );
832                 mod->sml_nvalues = NULL;
833                 *modtail = mod;
834                 modtail = &mod->sml_next;
835         }
836
837         *modtail = NULL;
838         return LDAP_SUCCESS;
839 }
840