]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
0f3f09a7ed58b0a5f8b9105b3d055e4c32070656
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 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   **modtail = &op->ora_modlist;
218         int             rc = 0;
219         BackendDB       *op_be, *bd = op->o_bd;
220         char            textbuf[ SLAP_TEXT_BUFLEN ];
221         size_t          textlen = sizeof( textbuf );
222
223         manageDSAit = get_manageDSAit( op );
224
225         /*
226          * We could be serving multiple database backends.  Select the
227          * appropriate one, or send a referral to our "referral server"
228          * if we don't hold it.
229          */
230         op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
231         if ( op->o_bd == NULL ) {
232                 op->o_bd = bd;
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                         send_ldap_result( op, rs );
239
240                         if ( rs->sr_ref != default_referral ) {
241                                 ber_bvarray_free( rs->sr_ref );
242                         }
243                 } else {
244                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
245                                 "no global superior knowledge" );
246                 }
247                 goto done;
248         }
249
250         /* If we've got a glued backend, check the real backend */
251         op_be = op->o_bd;
252         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
253                 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
254         }
255
256         /* check restrictions */
257         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
258                 send_ldap_result( op, rs );
259                 goto done;
260         }
261
262         /* check for referrals */
263         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
264                 goto done;
265         }
266
267         rs->sr_err = slap_mods_obsolete_check( op, op->ora_modlist,
268                 &rs->sr_text, textbuf, textlen );
269
270         if ( rs->sr_err != LDAP_SUCCESS ) {
271                 send_ldap_result( op, rs );
272                 goto done;
273         }
274
275         /*
276          * do the add if 1 && (2 || 3)
277          * 1) there is an add function implemented in this backend;
278          * 2) this backend is master for what it holds;
279          * 3) it's a replica and the dn supplied is the updatedn.
280          */
281         if ( op->o_bd->be_add ) {
282                 /* do the update here */
283                 int repl_user = be_isupdate( op );
284 #ifndef SLAPD_MULTIMASTER
285                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
286 #endif /* ! SLAPD_MULTIMASTER */
287                 {
288                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
289                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
290
291                         op->o_bd = op_be;
292
293                         if ( !update ) {
294                                 rs->sr_err = slap_mods_no_user_mod_check( op, op->ora_modlist,
295                                         &rs->sr_text, textbuf, textlen );
296
297                                 if ( rs->sr_err != LDAP_SUCCESS ) {
298                                         send_ldap_result( op, rs );
299                                         goto done;
300                                 }
301                         }
302
303                         if ( !repl_user ) {
304                                 /* go to the last mod */
305                                 for ( modtail = &op->ora_modlist;
306                                                 *modtail != NULL;
307                                                 modtail = &(*modtail)->sml_next )
308                                 {
309                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
310                                         assert( (*modtail)->sml_desc != NULL );
311                                 }
312
313
314                                 /* check for duplicate values */
315                                 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
316                                         op->ora_modlist, &rs->sr_text, textbuf, textlen );
317                                 if ( rs->sr_err != LDAP_SUCCESS ) {
318                                         send_ldap_result( op, rs );
319                                         goto done;
320                                 }
321
322                                 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
323                                         0, 0, &rs->sr_text, textbuf, textlen );
324                                 if ( rs->sr_err != LDAP_SUCCESS ) {
325                                         send_ldap_result( op, rs );
326                                         goto done;
327                                 }
328                         }
329
330 #ifdef SLAPD_MULTIMASTER
331                         if ( !repl_user )
332 #endif /* SLAPD_MULTIMASTER */
333                         {
334                                 cb.sc_next = op->o_callback;
335                                 op->o_callback = &cb;
336                         }
337                         rc = op->o_bd->be_add( op, rs );
338                         if ( rc == LDAP_SUCCESS ) {
339                                 /* NOTE: be_entry_release_w() is
340                                  * called by do_add(), so that global
341                                  * overlays on the way back can
342                                  * at least read the entry */
343                                 op->o_private = op->o_bd;
344                         }
345
346 #ifndef SLAPD_MULTIMASTER
347                 } else {
348                         BerVarray defref = NULL;
349
350                         defref = op->o_bd->be_update_refs
351                                 ? op->o_bd->be_update_refs : default_referral;
352
353                         if ( defref != NULL ) {
354                                 rs->sr_ref = referral_rewrite( defref,
355                                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
356                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
357                                 rs->sr_err = LDAP_REFERRAL;
358                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
359                                 send_ldap_result( op, rs );
360
361                                 if ( rs->sr_ref != default_referral ) {
362                                         ber_bvarray_free( rs->sr_ref );
363                                 }
364                         } else {
365                                 send_ldap_error( op, rs,
366                                         LDAP_UNWILLING_TO_PERFORM,
367                                         "shadow context; no update referral" );
368                         }
369 #endif /* SLAPD_MULTIMASTER */
370                 }
371         } else {
372                 Debug( LDAP_DEBUG_ARGS, "do_add: no backend support\n", 0, 0, 0 );
373                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
374                         "operation not supported within namingContext" );
375         }
376
377 done:;
378         op->o_bd = bd;
379         return rc;
380 }
381
382 int
383 slap_mods2entry(
384         Modifications *mods,
385         Entry **e,
386         int initial,
387         int dup,
388         const char **text,
389         char *textbuf, size_t textlen )
390 {
391         Attribute **tail;
392
393         if ( initial ) {
394                 assert( (*e)->e_attrs == NULL );
395         }
396
397         for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
398                 ;
399
400         *text = textbuf;
401
402         for( ; mods != NULL; mods = mods->sml_next ) {
403                 Attribute *attr;
404
405                 assert( mods->sml_desc != NULL );
406
407                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
408
409                 if( attr != NULL ) {
410 #define SLURPD_FRIENDLY
411 #ifdef SLURPD_FRIENDLY
412                         ber_len_t i,j;
413
414                         if ( !initial ) {
415                                 /*      
416                                  * This check allows overlays to override operational
417                                  * attributes by setting them directly in the entry.
418                                  * We assume slap_mods_no_user_mod_check() was called
419                                  * with the user modifications.
420                                  */
421                                 *text = NULL;
422                                 return LDAP_SUCCESS;
423                         }
424
425                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
426                                 /* count them */
427                         }
428                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
429                                 /* count them */
430                         }
431                         j++;    /* NULL */
432                         
433                         attr->a_vals = ch_realloc( attr->a_vals,
434                                 sizeof( struct berval ) * (i+j) );
435
436                         /* should check for duplicates */
437
438                         if ( dup ) {
439                                 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
440                                         ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
441                                 }
442                                 BER_BVZERO( &attr->a_vals[i+j] );
443                                 j++;
444                         } else {
445                                 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
446                                         sizeof( struct berval ) * j );
447                         }
448
449                         if( mods->sml_nvalues ) {
450                                 attr->a_nvals = ch_realloc( attr->a_nvals,
451                                         sizeof( struct berval ) * (i+j) );
452                                 if ( dup ) {
453                                         for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
454                                                 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
455                                         }
456                                         BER_BVZERO( &attr->a_nvals[i+j] );      
457                                 } else {
458                                         AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
459                                                 sizeof( struct berval ) * j );
460                                 }
461                         } else {
462                                 attr->a_nvals = attr->a_vals;
463                         }
464
465                         continue;
466 #else
467                         snprintf( textbuf, textlen,
468                                 "attribute '%s' provided more than once",
469                                 mods->sml_desc->ad_cname.bv_val );
470                         *text = textbuf;
471                         return LDAP_TYPE_OR_VALUE_EXISTS;
472 #endif
473                 }
474
475                 if( mods->sml_values[1].bv_val != NULL ) {
476                         /* check for duplicates */
477                         int             i, j, rc, match;
478                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
479
480                         for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
481                                 /* test asserted values against themselves */
482                                 for( j = 0; j < i; j++ ) {
483                                         rc = ordered_value_match( &match, mods->sml_desc, mr,
484                                                 SLAP_MR_EQUALITY
485                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
486                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
487                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
488                                                 mods->sml_nvalues
489                                                         ? &mods->sml_nvalues[i]
490                                                         : &mods->sml_values[i],
491                                                 mods->sml_nvalues
492                                                         ? &mods->sml_nvalues[j]
493                                                         : &mods->sml_values[j],
494                                                 text );
495
496                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
497                                                 /* value exists already */
498                                                 snprintf( textbuf, textlen,
499                                                         "%s: value #%d provided more than once",
500                                                         mods->sml_desc->ad_cname.bv_val, j );
501                                                 *text = textbuf;
502                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
503
504                                         } else if ( rc != LDAP_SUCCESS ) {
505                                                 return rc;
506                                         }
507                                 }
508                         }
509                 }
510
511                 attr = ch_calloc( 1, sizeof(Attribute) );
512
513                 /* move ad to attr structure */
514                 attr->a_desc = mods->sml_desc;
515
516                 /* move values to attr structure */
517                 /*      should check for duplicates */
518                 if ( dup ) { 
519                         int i;
520                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
521                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
522                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
523                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
524                         }
525                         BER_BVZERO( &attr->a_vals[i] );
526                 } else {
527                         attr->a_vals = mods->sml_values;
528                 }
529
530                 if ( mods->sml_nvalues ) {
531                         if ( dup ) {
532                                 int i;
533                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
534                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
535                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
536                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
537                                 }
538                                 BER_BVZERO( &attr->a_nvals[i] );
539                         } else {
540                                 attr->a_nvals = mods->sml_nvalues;
541                         }
542                 } else {
543                         attr->a_nvals = attr->a_vals;
544                 }
545
546                 *tail = attr;
547                 tail = &attr->a_next;
548         }
549
550         *text = NULL;
551
552         return LDAP_SUCCESS;
553 }
554
555 int
556 slap_entry2mods(
557         Entry *e,
558         Modifications **mods,
559         const char **text,
560         char *textbuf, size_t textlen )
561 {
562         Modifications   *modhead = NULL;
563         Modifications   *mod;
564         Modifications   **modtail = &modhead;
565         Attribute               *a_new;
566         AttributeDescription    *a_new_desc;
567         int                             i, count;
568
569         a_new = e->e_attrs;
570
571         while ( a_new != NULL ) {
572                 a_new_desc = a_new->a_desc;
573                 mod = (Modifications *) malloc( sizeof( Modifications ));
574                 
575                 mod->sml_op = LDAP_MOD_REPLACE;
576                 mod->sml_flags = 0;
577
578                 mod->sml_type = a_new_desc->ad_cname;
579
580                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
581
582                 mod->sml_values = (struct berval*) malloc(
583                         (count+1) * sizeof( struct berval) );
584
585                 /* see slap_mods_check() comments...
586                  * if a_vals == a_nvals, there is no normalizer.
587                  * in this case, mod->sml_nvalues must be left NULL.
588                  */
589                 if ( a_new->a_vals != a_new->a_nvals ) {
590                         mod->sml_nvalues = (struct berval*) malloc(
591                                 (count+1) * sizeof( struct berval) );
592                 } else {
593                         mod->sml_nvalues = NULL;
594                 }
595
596                 for ( i = 0; i < count; i++ ) {
597                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
598                         if ( mod->sml_nvalues ) {
599                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
600                         } 
601                 }
602
603                 mod->sml_values[count].bv_val = NULL; 
604                 mod->sml_values[count].bv_len = 0; 
605
606                 if ( mod->sml_nvalues ) {
607                         mod->sml_nvalues[count].bv_val = NULL; 
608                         mod->sml_nvalues[count].bv_len = 0; 
609                 }
610
611                 mod->sml_desc = a_new_desc;
612                 mod->sml_next =NULL;
613                 *modtail = mod;
614                 modtail = &mod->sml_next;
615                 a_new = a_new->a_next; 
616         }
617
618         *mods = modhead;
619
620         return LDAP_SUCCESS;
621 }
622
623 int slap_add_opattrs(
624         Operation *op,
625         const char **text,
626         char *textbuf,
627         size_t textlen,
628         int manage_ctxcsn )
629 {
630         struct berval name, timestamp, csn = BER_BVNULL;
631         struct berval nname, tmp;
632         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
633         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
634         Attribute *a;
635
636         a = attr_find( op->ora_e->e_attrs,
637                 slap_schema.si_ad_structuralObjectClass );
638
639         if ( !a ) {
640                 Attribute *oc;
641                 int rc;
642
643                 oc = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_objectClass );
644                 if ( oc ) {
645                         rc = structural_class( oc->a_vals, &tmp, NULL, text,
646                                 textbuf, textlen );
647                         if( rc != LDAP_SUCCESS ) return rc;
648
649                         attr_merge_one( op->ora_e, slap_schema.si_ad_structuralObjectClass,
650                                 &tmp, NULL );
651                 }
652         }
653
654         if ( SLAP_LASTMOD( op->o_bd ) ) {
655                 char *ptr;
656                 timestamp.bv_val = timebuf;
657                 if ( BER_BVISEMPTY( &op->o_csn )) {
658                         if ( SLAP_SHADOW( op->o_bd ))
659                                 manage_ctxcsn = 0;
660                         csn.bv_val = csnbuf;
661                         csn.bv_len = sizeof(csnbuf);
662                         slap_get_csn( op, &csn, manage_ctxcsn );
663                 } else {
664                         csn = op->o_csn;
665                 }
666                 ptr = ber_bvchr( &csn, '#' );
667                 if ( ptr ) {
668                         timestamp.bv_len = ptr - csn.bv_val;
669                         if ( timestamp.bv_len >= sizeof(timebuf) )      /* ?!? */
670                                 timestamp.bv_len = sizeof(timebuf) - 1;
671                         AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
672                         timebuf[timestamp.bv_len] = '\0';
673                 } else {
674                         time_t now = slap_get_time();
675
676                         timestamp.bv_len = sizeof(timebuf);
677
678                         slap_timestamp( &now, &timestamp );
679                 }
680
681                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
682                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
683                         nname = name;
684                 } else {
685                         name = op->o_dn;
686                         nname = op->o_ndn;
687                 }
688
689                 a = attr_find( op->ora_e->e_attrs,
690                         slap_schema.si_ad_entryUUID );
691                 if ( !a ) {
692                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
693
694                         tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
695                         tmp.bv_val = uuidbuf;
696                         
697                         attr_merge_normalize_one( op->ora_e,
698                                 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
699                 }
700
701                 a = attr_find( op->ora_e->e_attrs,
702                         slap_schema.si_ad_creatorsName );
703                 if ( !a ) {
704                         attr_merge_one( op->ora_e,
705                                 slap_schema.si_ad_creatorsName, &name, &nname );
706                 }
707
708                 a = attr_find( op->ora_e->e_attrs,
709                         slap_schema.si_ad_createTimestamp );
710                 if ( !a ) {
711                         attr_merge_one( op->ora_e,
712                                 slap_schema.si_ad_createTimestamp, &timestamp, NULL );
713                 }
714
715                 a = attr_find( op->ora_e->e_attrs,
716                         slap_schema.si_ad_entryCSN );
717                 if ( !a ) {
718                         attr_merge_one( op->ora_e,
719                                 slap_schema.si_ad_entryCSN, &csn, NULL );
720                 }
721
722                 a = attr_find( op->ora_e->e_attrs,
723                         slap_schema.si_ad_modifiersName );
724                 if ( !a ) {
725                         attr_merge_one( op->ora_e,
726                                 slap_schema.si_ad_modifiersName, &name, &nname );
727                 }
728
729                 a = attr_find( op->ora_e->e_attrs,
730                         slap_schema.si_ad_modifyTimestamp );
731                 if ( !a ) {
732                         attr_merge_one( op->ora_e,
733                                 slap_schema.si_ad_modifyTimestamp, &timestamp, NULL );
734                 }
735         }
736         return LDAP_SUCCESS;
737 }