]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
fix slapi build
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2002 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", LDAP_LEVEL_INFO, "do_modify: modify preOps failed\n"));
352 #else
353                 Debug (LDAP_DEBUG_TRACE, " modify preOps failed.\n", 0, 0, 0);
354 #endif
355         }
356 #endif /* defined( LDAP_SLAPI ) */
357
358         /*
359          * do the modify if 1 && (2 || 3)
360          * 1) there is a modify function implemented in this backend;
361          * 2) this backend is master for what it holds;
362          * 3) it's a replica and the dn supplied is the update_ndn.
363          */
364         if ( be->be_modify ) {
365                 /* do the update here */
366                 int repl_user = be_isupdate( be, &op->o_ndn );
367 #ifndef SLAPD_MULTIMASTER
368                 /* Multimaster slapd does not have to check for replicator dn
369                  * because it accepts each modify request
370                  */
371                 if ( !be->be_update_ndn.bv_len || repl_user )
372 #endif
373                 {
374                         int update = be->be_update_ndn.bv_len;
375                         const char *text;
376                         char textbuf[SLAP_TEXT_BUFLEN];
377                         size_t textlen = sizeof textbuf;
378
379                         rc = slap_mods_check( modlist, update, &text,
380                                 textbuf, textlen );
381
382                         if( rc != LDAP_SUCCESS ) {
383                                 send_ldap_result( conn, op, rc,
384                                         NULL, text, NULL, NULL );
385                                 goto cleanup;
386                         }
387
388                         if ( !repl_user ) {
389                                 for( modtail = &modlist;
390                                         *modtail != NULL;
391                                         modtail = &(*modtail)->sml_next )
392                                 {
393                                         /* empty */
394                                 }
395
396                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
397                                         textbuf, textlen );
398                                 if( rc != LDAP_SUCCESS ) {
399                                         send_ldap_result( conn, op, rc,
400                                                 NULL, text,
401                                                 NULL, NULL );
402                                         goto cleanup;
403                                 }
404                         }
405
406                         if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
407 #ifdef SLAPD_MULTIMASTER
408                                 && !repl_user
409 #endif
410                         ) {
411                                 /* but we log only the ones not from a replicator user */
412                                 replog( be, op, &pdn, &ndn, modlist );
413                         }
414
415 #ifndef SLAPD_MULTIMASTER
416                 /* send a referral */
417                 } else {
418                         BerVarray defref = be->be_update_refs
419                                 ? be->be_update_refs : default_referral;
420                         BerVarray ref = referral_rewrite( defref,
421                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
422
423                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
424                                 ref ? ref : defref, NULL );
425
426                         ber_bvarray_free( ref );
427 #endif
428                 }
429         } else {
430                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
431                     NULL, "operation not supported within namingContext",
432                         NULL, NULL );
433         }
434
435 #if defined( LDAP_SLAPI )
436         rc = doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb );
437         if ( rc != 0 && rc != LDAP_OTHER ) {
438                 /*
439                  * either there is no postOp (modify) plugins
440                  * or a plugin failed. Just log it
441                  *
442                  * FIXME: is this correct?
443                  */
444 #ifdef NEW_LOGGING
445                 LDAP_LOG(( "operation", LDAP_LEVEL_INFO, "do_modify: modify postOps failed\n"));
446 #else
447                 Debug (LDAP_DEBUG_TRACE, " modify postOps failed.\n", 0, 0, 0);
448 #endif
449         }
450 #endif /* defined( LDAP_SLAPI ) */
451
452 cleanup:
453         free( pdn.bv_val );
454         free( ndn.bv_val );
455         if ( modlist != NULL )
456                 slap_mods_free( modlist );
457         return rc;
458 }
459
460 /*
461  * Do basic attribute type checking and syntax validation.
462  */
463 int slap_mods_check(
464         Modifications *ml,
465         int update,
466         const char **text,
467         char *textbuf,
468         size_t textlen )
469 {
470         int rc;
471
472         for( ; ml != NULL; ml = ml->sml_next ) {
473                 AttributeDescription *ad = NULL;
474
475                 /* convert to attribute description */
476                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
477
478                 if( rc != LDAP_SUCCESS ) {
479                         snprintf( textbuf, textlen, "%s: %s",
480                                 ml->sml_type.bv_val, *text );
481                         *text = textbuf;
482                         return rc;
483                 }
484
485                 ad = ml->sml_desc;
486
487                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
488                         && !slap_ad_is_binary( ad ))
489                 {
490                         /* attribute requires binary transfer */
491                         snprintf( textbuf, textlen,
492                                 "%s: requires ;binary transfer",
493                                 ml->sml_type.bv_val );
494                         *text = textbuf;
495                         return LDAP_UNDEFINED_TYPE;
496                 }
497
498                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
499                         && slap_ad_is_binary( ad ))
500                 {
501                         /* attribute requires binary transfer */
502                         snprintf( textbuf, textlen,
503                                 "%s: disallows ;binary transfer",
504                                 ml->sml_type.bv_val );
505                         *text = textbuf;
506                         return LDAP_UNDEFINED_TYPE;
507                 }
508
509                 if( slap_ad_is_tag_range( ad )) {
510                         /* attribute requires binary transfer */
511                         snprintf( textbuf, textlen,
512                                 "%s: inappropriate use of tag range option",
513                                 ml->sml_type.bv_val );
514                         *text = textbuf;
515                         return LDAP_UNDEFINED_TYPE;
516                 }
517
518                 if (!update && is_at_no_user_mod( ad->ad_type )) {
519                         /* user modification disallowed */
520                         snprintf( textbuf, textlen,
521                                 "%s: no user modification allowed",
522                                 ml->sml_type.bv_val );
523                         *text = textbuf;
524                         return LDAP_CONSTRAINT_VIOLATION;
525                 }
526
527                 if ( is_at_obsolete( ad->ad_type ) &&
528                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
529                 {
530                         /*
531                          * attribute is obsolete,
532                          * only allow replace/delete with no values
533                          */
534                         snprintf( textbuf, textlen,
535                                 "%s: attribute is obsolete",
536                                 ml->sml_type.bv_val );
537                         *text = textbuf;
538                         return LDAP_CONSTRAINT_VIOLATION;
539                 }
540
541                 /*
542                  * check values
543                  */
544                 if( ml->sml_bvalues != NULL ) {
545                         ber_len_t nvals;
546                         slap_syntax_validate_func *validate =
547                                 ad->ad_type->sat_syntax->ssyn_validate;
548                         slap_syntax_transform_func *pretty =
549                                 ad->ad_type->sat_syntax->ssyn_pretty;
550  
551                         if( !pretty && !validate ) {
552                                 *text = "no validator for syntax";
553                                 snprintf( textbuf, textlen,
554                                         "%s: no validator for syntax %s",
555                                         ml->sml_type.bv_val,
556                                         ad->ad_type->sat_syntax->ssyn_oid );
557                                 *text = textbuf;
558                                 return LDAP_INVALID_SYNTAX;
559                         }
560
561                         /*
562                          * check that each value is valid per syntax
563                          *      and pretty if appropriate
564                          */
565                         for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
566                                 struct berval pval;
567                                 if( pretty ) {
568                                         rc = pretty( ad->ad_type->sat_syntax,
569                                                 &ml->sml_bvalues[nvals], &pval );
570                                 } else {
571                                         rc = validate( ad->ad_type->sat_syntax,
572                                                 &ml->sml_bvalues[nvals] );
573                                 }
574
575                                 if( rc != 0 ) {
576                                         snprintf( textbuf, textlen,
577                                                 "%s: value #%ld invalid per syntax",
578                                                 ml->sml_type.bv_val, (long) nvals );
579                                         *text = textbuf;
580                                         return LDAP_INVALID_SYNTAX;
581                                 }
582
583                                 if( pretty ) {
584                                         ber_memfree( ml->sml_bvalues[nvals].bv_val );
585                                         ml->sml_bvalues[nvals] = pval;
586                                 }
587                         }
588
589                         /*
590                          * a rough single value check... an additional check is needed
591                          * to catch add of single value to existing single valued attribute
592                          */
593                         if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
594                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
595                         {
596                                 snprintf( textbuf, textlen,
597                                         "%s: multiple value provided",
598                                         ml->sml_type.bv_val );
599                                 *text = textbuf;
600                                 return LDAP_CONSTRAINT_VIOLATION;
601                         }
602                 }
603         }
604
605         return LDAP_SUCCESS;
606 }
607
608 int slap_mods_opattrs(
609         Backend *be,
610         Operation *op,
611         Modifications *mods,
612         Modifications **modtail,
613         const char **text,
614         char *textbuf, size_t textlen )
615 {
616         struct berval name, timestamp, csn;
617         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
618         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
619         Modifications *mod;
620
621         int mop = op->o_tag == LDAP_REQ_ADD
622                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
623
624         assert( modtail != NULL );
625         assert( *modtail == NULL );
626
627         if( SLAP_LASTMOD(be) ) {
628                 struct tm *ltm;
629                 time_t now = slap_get_time();
630
631                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
632                 ltm = gmtime( &now );
633                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
634
635                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
636                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
637                 csn.bv_val = csnbuf;
638
639                 timestamp.bv_val = timebuf;
640                 timestamp.bv_len = strlen(timebuf);
641
642                 if( op->o_dn.bv_len == 0 ) {
643                         name.bv_val = SLAPD_ANONYMOUS;
644                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
645                 } else {
646                         name = op->o_dn;
647                 }
648         }
649
650         if( op->o_tag == LDAP_REQ_ADD ) {
651                 struct berval tmpval;
652
653                 if( global_schemacheck ) {
654                         int rc = mods_structural_class( mods, &tmpval,
655                                 text, textbuf, textlen );
656                         if( rc != LDAP_SUCCESS ) {
657                                 return rc;
658                         }
659
660                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
661                         mod->sml_op = mop;
662                         mod->sml_type.bv_val = NULL;
663                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
664                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
665                         ber_dupbv( &mod->sml_bvalues[0], &tmpval );
666                         mod->sml_bvalues[1].bv_val = NULL;
667                         assert( mod->sml_bvalues[0].bv_val );
668                         *modtail = mod;
669                         modtail = &mod->sml_next;
670                 }
671
672                 if( SLAP_LASTMOD(be) ) {
673                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
674
675                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
676                         tmpval.bv_val = uuidbuf;
677                 
678                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
679                         mod->sml_op = mop;
680                         mod->sml_type.bv_val = NULL;
681                         mod->sml_desc = slap_schema.si_ad_entryUUID;
682                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
683                         ber_dupbv( &mod->sml_bvalues[0], &tmpval );
684                         mod->sml_bvalues[1].bv_val = NULL;
685                         assert( mod->sml_bvalues[0].bv_val );
686                         *modtail = mod;
687                         modtail = &mod->sml_next;
688
689                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
690                         mod->sml_op = mop;
691                         mod->sml_type.bv_val = NULL;
692                         mod->sml_desc = slap_schema.si_ad_creatorsName;
693                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
694                         ber_dupbv( &mod->sml_bvalues[0], &name );
695                         mod->sml_bvalues[1].bv_val = NULL;
696                         assert( mod->sml_bvalues[0].bv_val );
697                         *modtail = mod;
698                         modtail = &mod->sml_next;
699
700                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
701                         mod->sml_op = mop;
702                         mod->sml_type.bv_val = NULL;
703                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
704                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
705                         ber_dupbv( &mod->sml_bvalues[0], &timestamp );
706                         mod->sml_bvalues[1].bv_val = NULL;
707                         assert( mod->sml_bvalues[0].bv_val );
708                         *modtail = mod;
709                         modtail = &mod->sml_next;
710                 }
711         }
712
713         if( SLAP_LASTMOD(be) ) {
714                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
715                 mod->sml_op = mop;
716                 mod->sml_type.bv_val = NULL;
717                 mod->sml_desc = slap_schema.si_ad_entryCSN;
718                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
719                 ber_dupbv( &mod->sml_bvalues[0], &csn );
720                 mod->sml_bvalues[1].bv_val = NULL;
721                 assert( mod->sml_bvalues[0].bv_val );
722                 *modtail = mod;
723                 modtail = &mod->sml_next;
724
725                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
726                 mod->sml_op = mop;
727                 mod->sml_type.bv_val = NULL;
728                 mod->sml_desc = slap_schema.si_ad_modifiersName;
729                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
730                 ber_dupbv( &mod->sml_bvalues[0], &name );
731                 mod->sml_bvalues[1].bv_val = NULL;
732                 assert( mod->sml_bvalues[0].bv_val );
733                 *modtail = mod;
734                 modtail = &mod->sml_next;
735
736                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
737                 mod->sml_op = mop;
738                 mod->sml_type.bv_val = NULL;
739                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
740                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
741                 ber_dupbv( &mod->sml_bvalues[0], &timestamp );
742                 mod->sml_bvalues[1].bv_val = NULL;
743                 assert( mod->sml_bvalues[0].bv_val );
744                 *modtail = mod;
745                 modtail = &mod->sml_next;
746         }
747
748         *modtail = NULL;
749         return LDAP_SUCCESS;
750 }