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