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