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