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