]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
don't trust strchr/strrchr with bervals
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31 #include <ac/socket.h>
32
33 #include "lutil.h"
34 #include "slap.h"
35
36 int
37 do_add( Operation *op, SlapReply *rs )
38 {
39         BerElement      *ber = op->o_ber;
40         char            *last;
41         struct berval   dn = BER_BVNULL;
42         ber_len_t       len;
43         ber_tag_t       tag;
44         Modifications   *modlist = NULL;
45         Modifications   **modtail = &modlist;
46         Modifications   tmp;
47         char            textbuf[ SLAP_TEXT_BUFLEN ];
48         size_t          textlen = sizeof( textbuf );
49         int             rc = 0;
50         int             freevals = 1;
51
52         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
53         /*
54          * Parse the add request.  It looks like this:
55          *
56          *      AddRequest := [APPLICATION 14] SEQUENCE {
57          *              name    DistinguishedName,
58          *              attrs   SEQUENCE OF SEQUENCE {
59          *                      type    AttributeType,
60          *                      values  SET OF AttributeValue
61          *              }
62          *      }
63          */
64
65         /* get the name */
66         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
67                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
68                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
69                 return SLAPD_DISCONNECT;
70         }
71
72         op->ora_e = (Entry *) ch_calloc( 1, sizeof(Entry) );
73
74         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
75                 op->o_tmpmemctx );
76
77         if ( rs->sr_err != LDAP_SUCCESS ) {
78                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
79                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
80                 goto done;
81         }
82
83         ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
84         ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
85
86         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", op->ora_e->e_dn, 0, 0 );
87
88         /* get the attrs */
89         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
90             tag = ber_next_element( ber, &len, last ) )
91         {
92                 Modifications *mod;
93                 ber_tag_t rtag;
94
95                 tmp.sml_nvalues = NULL;
96
97                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
98
99                 if ( rtag == LBER_ERROR ) {
100                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
101                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
102                         rs->sr_err = SLAPD_DISCONNECT;
103                         goto done;
104                 }
105
106                 if ( tmp.sml_values == NULL ) {
107                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
108                                 tmp.sml_type.bv_val, 0, 0 );
109                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
110                                 "no values for attribute type" );
111                         goto done;
112                 }
113
114                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
115                 mod->sml_op = LDAP_MOD_ADD;
116                 mod->sml_flags = 0;
117                 mod->sml_next = NULL;
118                 mod->sml_desc = NULL;
119                 mod->sml_type = tmp.sml_type;
120                 mod->sml_values = tmp.sml_values;
121                 mod->sml_nvalues = NULL;
122
123                 *modtail = mod;
124                 modtail = &mod->sml_next;
125         }
126
127         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
128                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
129                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
130                 rs->sr_err = SLAPD_DISCONNECT;
131                 goto done;
132         }
133
134         if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
135                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
136                 goto done;
137         } 
138
139         if ( modlist == NULL ) {
140                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
141                         "no attributes provided" );
142                 goto done;
143         }
144
145         Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
146             op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
147
148         if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
149                 /* protocolError may be a more appropriate error */
150                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
151                         "root DSE already exists" );
152                 goto done;
153
154         } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
155                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
156                         "subschema subentry already exists" );
157                 goto done;
158         }
159
160         rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
161                 textbuf, textlen, NULL );
162
163         if ( rs->sr_err != LDAP_SUCCESS ) {
164                 send_ldap_result( op, rs );
165                 goto done;
166         }
167
168         /* temporary; remove if not invoking backend function */
169         op->ora_modlist = modlist;
170
171         /* call this so global overlays/SLAPI have access to ora_e */
172         rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
173                 1, 0, &rs->sr_text, textbuf, textlen );
174         if ( rs->sr_err != LDAP_SUCCESS ) {
175                 send_ldap_result( op, rs );
176                 goto done;
177         }
178
179         freevals = 0;
180
181         op->o_bd = frontendDB;
182         rc = frontendDB->be_add( op, rs );
183         if ( rc == 0 ) {
184                 if ( op->ora_e != NULL && op->o_private != NULL ) {
185                         BackendDB       *bd = op->o_bd;
186
187                         op->o_bd = (BackendDB *)op->o_private;
188                         op->o_private = NULL;
189
190                         be_entry_release_w( op, op->ora_e );
191
192                         op->ora_e = NULL;
193                         op->o_bd = bd;
194                         op->o_private = NULL;
195                 }
196         }
197
198 done:;
199         if ( modlist != NULL ) {
200                 /* in case of error, free the values as well */
201                 slap_mods_free( modlist, freevals );
202         }
203
204         if ( op->ora_e != NULL ) {
205                 entry_free( op->ora_e );
206         }
207         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
208         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
209
210         return rc;
211 }
212
213 int
214 fe_op_add( Operation *op, SlapReply *rs )
215 {
216         int             manageDSAit;
217         Modifications   *modlist = op->ora_modlist;
218         Modifications   **modtail = &modlist;
219         int             rc = 0;
220         BackendDB *op_be;
221         char            textbuf[ SLAP_TEXT_BUFLEN ];
222         size_t          textlen = sizeof( textbuf );
223
224         manageDSAit = get_manageDSAit( op );
225
226         /*
227          * We could be serving multiple database backends.  Select the
228          * appropriate one, or send a referral to our "referral server"
229          * if we don't hold it.
230          */
231         op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
232         if ( op->o_bd == NULL ) {
233                 rs->sr_ref = referral_rewrite( default_referral,
234                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
235                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
236                 if ( rs->sr_ref ) {
237                         rs->sr_err = LDAP_REFERRAL;
238                         op->o_bd = frontendDB;
239                         send_ldap_result( op, rs );
240                         op->o_bd = NULL;
241
242                         if ( rs->sr_ref != default_referral ) {
243                                 ber_bvarray_free( rs->sr_ref );
244                         }
245                 } else {
246                         op->o_bd = frontendDB;
247                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
248                                 "no global superior knowledge" );
249                         op->o_bd = NULL;
250                 }
251                 goto done;
252         }
253
254         /* If we've got a glued backend, check the real backend */
255         op_be = op->o_bd;
256         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
257                 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
258         }
259
260         /* check restrictions */
261         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
262                 send_ldap_result( op, rs );
263                 goto done;
264         }
265
266         /* check for referrals */
267         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
268                 goto done;
269         }
270
271         rs->sr_err = slap_mods_obsolete_check( op, modlist,
272                 &rs->sr_text, textbuf, textlen );
273
274         if ( rs->sr_err != LDAP_SUCCESS ) {
275                 send_ldap_result( op, rs );
276                 goto done;
277         }
278
279         /*
280          * do the add if 1 && (2 || 3)
281          * 1) there is an add function implemented in this backend;
282          * 2) this backend is master for what it holds;
283          * 3) it's a replica and the dn supplied is the updatedn.
284          */
285         if ( op->o_bd->be_add ) {
286                 /* do the update here */
287                 int repl_user = be_isupdate( op );
288 #ifndef SLAPD_MULTIMASTER
289                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
290 #endif
291                 {
292                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
293                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
294
295                         op->o_bd = op_be;
296
297                         if ( !update ) {
298                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
299                                         &rs->sr_text, textbuf, textlen );
300
301                                 if ( rs->sr_err != LDAP_SUCCESS ) {
302                                         send_ldap_result( op, rs );
303                                         goto done;
304                                 }
305                         }
306
307                         if ( !repl_user ) {
308                                 /* go to the last mod */
309                                 for ( modtail = &modlist;
310                                                 *modtail != NULL;
311                                                 modtail = &(*modtail)->sml_next )
312                                 {
313                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
314                                         assert( (*modtail)->sml_desc != NULL );
315                                 }
316
317
318                                 /* check for duplicate values */
319                                 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
320                                         modlist, &rs->sr_text, textbuf, textlen );
321                                 if ( rs->sr_err != LDAP_SUCCESS ) {
322                                         send_ldap_result( op, rs );
323                                         goto done;
324                                 }
325
326                                 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
327                                         0, 0, &rs->sr_text, textbuf, textlen );
328                                 if ( rs->sr_err != LDAP_SUCCESS ) {
329                                         send_ldap_result( op, rs );
330                                         goto done;
331                                 }
332                         }
333
334 #ifdef SLAPD_MULTIMASTER
335                         if ( !repl_user )
336 #endif
337                         {
338                                 cb.sc_next = op->o_callback;
339                                 op->o_callback = &cb;
340                         }
341                         rc = op->o_bd->be_add( op, rs );
342                         if ( rc == LDAP_SUCCESS ) {
343                                 /* NOTE: be_entry_release_w() is
344                                  * called by do_add(), so that global
345                                  * overlays on the way back can
346                                  * at least read the entry */
347                                 op->o_private = op->o_bd;
348                         }
349
350 #ifndef SLAPD_MULTIMASTER
351                 } else {
352                         BerVarray defref = NULL;
353
354                         defref = op->o_bd->be_update_refs
355                                 ? op->o_bd->be_update_refs : default_referral;
356
357                         if ( defref != NULL ) {
358                                 rs->sr_ref = referral_rewrite( defref,
359                                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
360                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
361                                 rs->sr_err = LDAP_REFERRAL;
362                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
363                                 send_ldap_result( op, rs );
364
365                                 if ( rs->sr_ref != default_referral ) {
366                                         ber_bvarray_free( rs->sr_ref );
367                                 }
368                         } else {
369                                 send_ldap_error( op, rs,
370                                         LDAP_UNWILLING_TO_PERFORM,
371                                         "shadow context; no update referral" );
372                         }
373 #endif /* SLAPD_MULTIMASTER */
374                 }
375         } else {
376             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
377             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
378                         "operation not supported within namingContext" );
379         }
380
381 done:;
382         return rc;
383 }
384
385 int
386 slap_mods2entry(
387         Modifications *mods,
388         Entry **e,
389         int initial,
390         int dup,
391         const char **text,
392         char *textbuf, size_t textlen )
393 {
394         Attribute **tail;
395
396         if ( initial ) {
397                 assert( (*e)->e_attrs == NULL );
398         }
399
400         for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
401                 ;
402
403         *text = textbuf;
404
405         for( ; mods != NULL; mods = mods->sml_next ) {
406                 Attribute *attr;
407
408                 assert( mods->sml_desc != NULL );
409
410                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
411
412                 if( attr != NULL ) {
413 #define SLURPD_FRIENDLY
414 #ifdef SLURPD_FRIENDLY
415                         ber_len_t i,j;
416
417                         if ( !initial ) {
418                                 /*      
419                                  * This check allows overlays to override operational
420                                  * attributes by setting them directly in the entry.
421                                  * We assume slap_mods_no_user_mod_check() was called
422                                  * with the user modifications.
423                                  */
424                                 *text = NULL;
425                                 return LDAP_SUCCESS;
426                         }
427
428                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
429                                 /* count them */
430                         }
431                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
432                                 /* count them */
433                         }
434                         j++;    /* NULL */
435                         
436                         attr->a_vals = ch_realloc( attr->a_vals,
437                                 sizeof( struct berval ) * (i+j) );
438
439                         /* should check for duplicates */
440
441                         if ( dup ) {
442                                 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
443                                         ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
444                                 }
445                                 BER_BVZERO( &attr->a_vals[i+j] );
446                                 j++;
447                         } else {
448                                 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
449                                         sizeof( struct berval ) * j );
450                         }
451
452                         if( mods->sml_nvalues ) {
453                                 attr->a_nvals = ch_realloc( attr->a_nvals,
454                                         sizeof( struct berval ) * (i+j) );
455                                 if ( dup ) {
456                                         for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
457                                                 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
458                                         }
459                                         BER_BVZERO( &attr->a_nvals[i+j] );      
460                                 } else {
461                                         AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
462                                                 sizeof( struct berval ) * j );
463                                 }
464                         } else {
465                                 attr->a_nvals = attr->a_vals;
466                         }
467
468                         continue;
469 #else
470                         snprintf( textbuf, textlen,
471                                 "attribute '%s' provided more than once",
472                                 mods->sml_desc->ad_cname.bv_val );
473                         return LDAP_TYPE_OR_VALUE_EXISTS;
474 #endif
475                 }
476
477                 if( mods->sml_values[1].bv_val != NULL ) {
478                         /* check for duplicates */
479                         int             i, j, rc, match;
480                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
481
482                         for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
483                                 /* test asserted values against themselves */
484                                 for( j = 0; j < i; j++ ) {
485                                         rc = ordered_value_match( &match, mods->sml_desc, mr,
486                                                 SLAP_MR_EQUALITY
487                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
488                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
489                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
490                                                 mods->sml_nvalues
491                                                         ? &mods->sml_nvalues[i]
492                                                         : &mods->sml_values[i],
493                                                 mods->sml_nvalues
494                                                         ? &mods->sml_nvalues[j]
495                                                         : &mods->sml_values[j],
496                                                 text );
497
498                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
499                                                 /* value exists already */
500                                                 snprintf( textbuf, textlen,
501                                                         "%s: value #%d provided more than once",
502                                                         mods->sml_desc->ad_cname.bv_val, j );
503                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
504
505                                         } else if ( rc != LDAP_SUCCESS ) {
506                                                 return rc;
507                                         }
508                                 }
509                         }
510                 }
511
512                 attr = ch_calloc( 1, sizeof(Attribute) );
513
514                 /* move ad to attr structure */
515                 attr->a_desc = mods->sml_desc;
516
517                 /* move values to attr structure */
518                 /*      should check for duplicates */
519                 if ( dup ) { 
520                         int i;
521                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
522                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
523                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
524                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
525                         }
526                         BER_BVZERO( &attr->a_vals[i] );
527                 } else {
528                         attr->a_vals = mods->sml_values;
529                 }
530
531                 if ( mods->sml_nvalues ) {
532                         if ( dup ) {
533                                 int i;
534                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
535                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
536                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
537                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
538                                 }
539                                 BER_BVZERO( &attr->a_nvals[i] );
540                         } else {
541                                 attr->a_nvals = mods->sml_nvalues;
542                         }
543                 } else {
544                         attr->a_nvals = attr->a_vals;
545                 }
546
547                 *tail = attr;
548                 tail = &attr->a_next;
549         }
550
551         *text = NULL;
552
553         return LDAP_SUCCESS;
554 }
555
556 int
557 slap_entry2mods(
558         Entry *e,
559         Modifications **mods,
560         const char **text,
561         char *textbuf, size_t textlen )
562 {
563         Modifications   *modhead = NULL;
564         Modifications   *mod;
565         Modifications   **modtail = &modhead;
566         Attribute               *a_new;
567         AttributeDescription    *a_new_desc;
568         int                             i, count;
569
570         a_new = e->e_attrs;
571
572         while ( a_new != NULL ) {
573                 a_new_desc = a_new->a_desc;
574                 mod = (Modifications *) malloc( sizeof( Modifications ));
575                 
576                 mod->sml_op = LDAP_MOD_REPLACE;
577                 mod->sml_flags = 0;
578
579                 mod->sml_type = a_new_desc->ad_cname;
580
581                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
582
583                 mod->sml_values = (struct berval*) malloc(
584                         (count+1) * sizeof( struct berval) );
585
586                 /* see slap_mods_check() comments...
587                  * if a_vals == a_nvals, there is no normalizer.
588                  * in this case, mod->sml_nvalues must be left NULL.
589                  */
590                 if ( a_new->a_vals != a_new->a_nvals ) {
591                         mod->sml_nvalues = (struct berval*) malloc(
592                                 (count+1) * sizeof( struct berval) );
593                 } else {
594                         mod->sml_nvalues = NULL;
595                 }
596
597                 for ( i = 0; i < count; i++ ) {
598                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
599                         if ( mod->sml_nvalues ) {
600                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
601                         } 
602                 }
603
604                 mod->sml_values[count].bv_val = NULL; 
605                 mod->sml_values[count].bv_len = 0; 
606
607                 if ( mod->sml_nvalues ) {
608                         mod->sml_nvalues[count].bv_val = NULL; 
609                         mod->sml_nvalues[count].bv_len = 0; 
610                 }
611
612                 mod->sml_desc = a_new_desc;
613                 mod->sml_next =NULL;
614                 *modtail = mod;
615                 modtail = &mod->sml_next;
616                 a_new = a_new->a_next; 
617         }
618
619         *mods = modhead;
620
621         return LDAP_SUCCESS;
622 }
623
624 int slap_add_opattrs(
625         Operation *op,
626         const char **text,
627         char *textbuf,
628         size_t textlen,
629         int manage_ctxcsn )
630 {
631         struct berval name, timestamp, csn = BER_BVNULL;
632         struct berval nname, tmp;
633         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
634         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
635         Attribute *a;
636
637         a = attr_find( op->ora_e->e_attrs,
638                 slap_schema.si_ad_structuralObjectClass );
639
640         if ( !a ) {
641                 Attribute *oc;
642                 int rc;
643
644                 oc = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_objectClass );
645                 if ( oc ) {
646                         rc = structural_class( oc->a_vals, &tmp, NULL, text,
647                                 textbuf, textlen );
648                         if( rc != LDAP_SUCCESS ) return rc;
649
650                         attr_merge_one( op->ora_e, slap_schema.si_ad_structuralObjectClass,
651                                 &tmp, NULL );
652                 }
653         }
654
655         if ( SLAP_LASTMOD( op->o_bd ) ) {
656                 char *ptr;
657                 timestamp.bv_val = timebuf;
658                 if ( BER_BVISEMPTY( &op->o_csn )) {
659                         if ( SLAP_SHADOW( op->o_bd ))
660                                 manage_ctxcsn = 0;
661                         csn.bv_val = csnbuf;
662                         csn.bv_len = sizeof(csnbuf);
663                         slap_get_csn( op, &csn, manage_ctxcsn );
664                 } else {
665                         csn = op->o_csn;
666                 }
667                 ptr = ber_bvchr( &csn, '#' );
668                 if ( ptr ) {
669                         timestamp.bv_len = ptr - csn.bv_val;
670                         if ( timestamp.bv_len >= sizeof(timebuf) )
671                                 timestamp.bv_len = sizeof(timebuf) - 1;
672                         strncpy( timebuf, csn.bv_val, timestamp.bv_len );
673                         timebuf[timestamp.bv_len] = '\0';
674                 } else {
675                         time_t now = slap_get_time();
676
677                         timestamp.bv_len = sizeof(timebuf);
678
679                         slap_timestamp( &now, &timestamp );
680                 }
681
682                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
683                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
684                         nname = name;
685                 } else {
686                         name = op->o_dn;
687                         nname = op->o_ndn;
688                 }
689
690                 a = attr_find( op->ora_e->e_attrs,
691                         slap_schema.si_ad_entryUUID );
692                 if ( !a ) {
693                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
694
695                         tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
696                         tmp.bv_val = uuidbuf;
697                         
698                         attr_merge_normalize_one( op->ora_e,
699                                 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
700                 }
701
702                 a = attr_find( op->ora_e->e_attrs,
703                         slap_schema.si_ad_creatorsName );
704                 if ( !a ) {
705                         attr_merge_one( op->ora_e,
706                                 slap_schema.si_ad_creatorsName, &name, &nname );
707                 }
708
709                 a = attr_find( op->ora_e->e_attrs,
710                         slap_schema.si_ad_createTimestamp );
711                 if ( !a ) {
712                         attr_merge_one( op->ora_e,
713                                 slap_schema.si_ad_createTimestamp, &timestamp, NULL );
714                 }
715
716                 a = attr_find( op->ora_e->e_attrs,
717                         slap_schema.si_ad_entryCSN );
718                 if ( !a ) {
719                         attr_merge_one( op->ora_e,
720                                 slap_schema.si_ad_entryCSN, &csn, NULL );
721                 }
722
723                 a = attr_find( op->ora_e->e_attrs,
724                         slap_schema.si_ad_modifiersName );
725                 if ( !a ) {
726                         attr_merge_one( op->ora_e,
727                                 slap_schema.si_ad_modifiersName, &name, &nname );
728                 }
729
730                 a = attr_find( op->ora_e->e_attrs,
731                         slap_schema.si_ad_modifyTimestamp );
732                 if ( !a ) {
733                         attr_merge_one( op->ora_e,
734                                 slap_schema.si_ad_modifyTimestamp, &timestamp, NULL );
735                 }
736
737         }
738         return LDAP_SUCCESS;
739 }