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