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