]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
cleanup and document ITS#4781 commit
[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_alloc();
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
184 #ifdef LDAP_X_TXN
185         if ( rc == LDAP_X_TXN_SPECIFY_OKAY ) {
186                 /* skip cleanup */
187                 return rc;
188         } else
189 #endif
190         if ( rc == 0 ) {
191                 if ( op->ora_e != NULL && op->o_private != NULL ) {
192                         BackendDB       *bd = op->o_bd;
193
194                         op->o_bd = (BackendDB *)op->o_private;
195                         op->o_private = NULL;
196
197                         be_entry_release_w( op, op->ora_e );
198
199                         op->ora_e = NULL;
200                         op->o_bd = bd;
201                         op->o_private = NULL;
202                 }
203         }
204
205 done:;
206         if ( modlist != NULL ) {
207                 /* in case of error, free the values as well */
208                 slap_mods_free( modlist, freevals );
209         }
210
211         if ( op->ora_e != NULL ) {
212                 entry_free( op->ora_e );
213         }
214         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
215         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
216
217         return rc;
218 }
219
220 int
221 fe_op_add( Operation *op, SlapReply *rs )
222 {
223         int             manageDSAit;
224         Modifications   **modtail = &op->ora_modlist;
225         int             rc = 0;
226         BackendDB       *op_be, *bd = op->o_bd;
227         char            textbuf[ SLAP_TEXT_BUFLEN ];
228         size_t          textlen = sizeof( textbuf );
229
230         manageDSAit = get_manageDSAit( op );
231
232         /*
233          * We could be serving multiple database backends.  Select the
234          * appropriate one, or send a referral to our "referral server"
235          * if we don't hold it.
236          */
237         op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
238         if ( op->o_bd == NULL ) {
239                 op->o_bd = bd;
240                 rs->sr_ref = referral_rewrite( default_referral,
241                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
242                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
243                 if ( rs->sr_ref ) {
244                         rs->sr_err = LDAP_REFERRAL;
245                         send_ldap_result( op, rs );
246
247                         if ( rs->sr_ref != default_referral ) {
248                                 ber_bvarray_free( rs->sr_ref );
249                         }
250                 } else {
251                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
252                                 "no global superior knowledge" );
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, op->ora_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                 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
292                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
293                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
294
295                         op->o_bd = op_be;
296
297                         if ( !update ) {
298                                 rs->sr_err = slap_mods_no_user_mod_check( op, op->ora_modlist,
299                                         &rs->sr_text, textbuf, textlen );
300
301                                 if ( rs->sr_err != LDAP_SUCCESS ) {
302                                         send_ldap_result( op, rs );
303                                         goto done;
304                                 }
305                         }
306
307                         if ( !repl_user ) {
308                                 /* go to the last mod */
309                                 for ( modtail = &op->ora_modlist;
310                                                 *modtail != NULL;
311                                                 modtail = &(*modtail)->sml_next )
312                                 {
313                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
314                                         assert( (*modtail)->sml_desc != NULL );
315                                 }
316
317
318                                 /* check for unmodifiable attributes */
319                                 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
320                                         op->ora_modlist, &rs->sr_text, textbuf, textlen );
321                                 if ( rs->sr_err != LDAP_SUCCESS ) {
322                                         send_ldap_result( op, rs );
323                                         goto done;
324                                 }
325
326 #if 0                   /* This is a no-op since *modtail is NULL */
327                                 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
328                                         0, 0, &rs->sr_text, textbuf, textlen );
329                                 if ( rs->sr_err != LDAP_SUCCESS ) {
330                                         send_ldap_result( op, rs );
331                                         goto done;
332                                 }
333 #endif
334
335                                 cb.sc_next = op->o_callback;
336                                 op->o_callback = &cb;
337                         }
338
339                         rc = op->o_bd->be_add( op, rs );
340                         if ( rc == LDAP_SUCCESS ) {
341                                 /* NOTE: be_entry_release_w() is
342                                  * called by do_add(), so that global
343                                  * overlays on the way back can
344                                  * at least read the entry */
345                                 op->o_private = op->o_bd;
346                         }
347
348                 } else {
349                         BerVarray defref = NULL;
350
351                         defref = op->o_bd->be_update_refs
352                                 ? op->o_bd->be_update_refs : default_referral;
353
354                         if ( defref != NULL ) {
355                                 rs->sr_ref = referral_rewrite( defref,
356                                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
357                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
358                                 rs->sr_err = LDAP_REFERRAL;
359                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
360                                 send_ldap_result( op, rs );
361
362                                 if ( rs->sr_ref != default_referral ) {
363                                         ber_bvarray_free( rs->sr_ref );
364                                 }
365                         } else {
366                                 send_ldap_error( op, rs,
367                                         LDAP_UNWILLING_TO_PERFORM,
368                                         "shadow context; no update referral" );
369                         }
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                         /* checked for duplicates in slap_mods_check */
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 0   /* checked for duplicates in slap_mods_check */
476                 if( mods->sml_values[1].bv_val != NULL ) {
477                         /* check for duplicates */
478                         int             i, j, rc, match;
479                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
480
481                         for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
482                                 /* test asserted values against themselves */
483                                 for( j = 0; j < i; j++ ) {
484                                         rc = ordered_value_match( &match, mods->sml_desc, mr,
485                                                 SLAP_MR_EQUALITY
486                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
487                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
488                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
489                                                 mods->sml_nvalues
490                                                         ? &mods->sml_nvalues[i]
491                                                         : &mods->sml_values[i],
492                                                 mods->sml_nvalues
493                                                         ? &mods->sml_nvalues[j]
494                                                         : &mods->sml_values[j],
495                                                 text );
496
497                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
498                                                 /* value exists already */
499                                                 snprintf( textbuf, textlen,
500                                                         "%s: value #%d provided more than once",
501                                                         mods->sml_desc->ad_cname.bv_val, j );
502                                                 *text = textbuf;
503                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
504
505                                         } else if ( rc != LDAP_SUCCESS ) {
506                                                 return rc;
507                                         }
508                                 }
509                         }
510                 }
511 #endif
512
513                 attr = attr_alloc( mods->sml_desc );
514
515                 /* move values to attr structure */
516                 if ( dup ) { 
517                         int i;
518                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
519                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
520                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
521                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
522                         }
523                         BER_BVZERO( &attr->a_vals[i] );
524                 } else {
525                         attr->a_vals = mods->sml_values;
526                 }
527
528                 if ( mods->sml_nvalues ) {
529                         if ( dup ) {
530                                 int i;
531                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
532                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
533                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
534                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
535                                 }
536                                 BER_BVZERO( &attr->a_nvals[i] );
537                         } else {
538                                 attr->a_nvals = mods->sml_nvalues;
539                         }
540                 } else {
541                         attr->a_nvals = attr->a_vals;
542                 }
543
544                 *tail = attr;
545                 tail = &attr->a_next;
546         }
547
548         *text = NULL;
549
550         return LDAP_SUCCESS;
551 }
552
553 int
554 slap_entry2mods(
555         Entry *e,
556         Modifications **mods,
557         const char **text,
558         char *textbuf, size_t textlen )
559 {
560         Modifications   *modhead = NULL;
561         Modifications   *mod;
562         Modifications   **modtail = &modhead;
563         Attribute               *a_new;
564         AttributeDescription    *a_new_desc;
565         int                             i, count;
566
567         a_new = e->e_attrs;
568
569         while ( a_new != NULL ) {
570                 a_new_desc = a_new->a_desc;
571                 mod = (Modifications *) malloc( sizeof( Modifications ));
572                 
573                 mod->sml_op = LDAP_MOD_REPLACE;
574                 mod->sml_flags = 0;
575
576                 mod->sml_type = a_new_desc->ad_cname;
577
578                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
579
580                 mod->sml_values = (struct berval*) malloc(
581                         (count+1) * sizeof( struct berval) );
582
583                 /* see slap_mods_check() comments...
584                  * if a_vals == a_nvals, there is no normalizer.
585                  * in this case, mod->sml_nvalues must be left NULL.
586                  */
587                 if ( a_new->a_vals != a_new->a_nvals ) {
588                         mod->sml_nvalues = (struct berval*) malloc(
589                                 (count+1) * sizeof( struct berval) );
590                 } else {
591                         mod->sml_nvalues = NULL;
592                 }
593
594                 for ( i = 0; i < count; i++ ) {
595                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
596                         if ( mod->sml_nvalues ) {
597                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
598                         } 
599                 }
600
601                 mod->sml_values[count].bv_val = NULL; 
602                 mod->sml_values[count].bv_len = 0; 
603
604                 if ( mod->sml_nvalues ) {
605                         mod->sml_nvalues[count].bv_val = NULL; 
606                         mod->sml_nvalues[count].bv_len = 0; 
607                 }
608
609                 mod->sml_desc = a_new_desc;
610                 mod->sml_next =NULL;
611                 *modtail = mod;
612                 modtail = &mod->sml_next;
613                 a_new = a_new->a_next; 
614         }
615
616         *mods = modhead;
617
618         return LDAP_SUCCESS;
619 }
620
621 int slap_add_opattrs(
622         Operation *op,
623         const char **text,
624         char *textbuf,
625         size_t textlen,
626         int manage_ctxcsn )
627 {
628         struct berval name, timestamp, csn = BER_BVNULL;
629         struct berval nname, tmp;
630         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
631         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
632         Attribute *a;
633
634         if ( SLAP_LASTMOD( op->o_bd ) ) {
635                 char *ptr;
636                 int gotcsn = 0;
637
638                 timestamp.bv_val = timebuf;
639                 a = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_entryCSN );
640                 if ( a ) {
641                         gotcsn = 1;
642                         csn = a->a_vals[0];
643                 }
644                 if ( BER_BVISEMPTY( &op->o_csn )) {
645                         if ( !gotcsn ) {
646                                 csn.bv_val = csnbuf;
647                                 csn.bv_len = sizeof(csnbuf);
648                                 slap_get_csn( op, &csn, manage_ctxcsn );
649                         } else {
650                                 if ( manage_ctxcsn )
651                                         slap_queue_csn( op, &csn );
652                         }
653                 } else {
654                         csn = op->o_csn;
655                 }
656                 ptr = ber_bvchr( &csn, '#' );
657                 if ( ptr ) {
658                         timestamp.bv_len = ptr - csn.bv_val;
659                         if ( timestamp.bv_len >= sizeof(timebuf) )      /* ?!? */
660                                 timestamp.bv_len = sizeof(timebuf) - 1;
661                         AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
662                         timebuf[timestamp.bv_len] = '\0';
663                 } else {
664                         time_t now = slap_get_time();
665
666                         timestamp.bv_len = sizeof(timebuf);
667
668                         slap_timestamp( &now, &timestamp );
669                 }
670
671                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
672                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
673                         nname = name;
674                 } else {
675                         name = op->o_dn;
676                         nname = op->o_ndn;
677                 }
678
679                 a = attr_find( op->ora_e->e_attrs,
680                         slap_schema.si_ad_entryUUID );
681                 if ( !a ) {
682                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
683
684                         tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
685                         tmp.bv_val = uuidbuf;
686                         
687                         attr_merge_normalize_one( op->ora_e,
688                                 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
689                 }
690
691                 a = attr_find( op->ora_e->e_attrs,
692                         slap_schema.si_ad_creatorsName );
693                 if ( !a ) {
694                         attr_merge_one( op->ora_e,
695                                 slap_schema.si_ad_creatorsName, &name, &nname );
696                 }
697
698                 a = attr_find( op->ora_e->e_attrs,
699                         slap_schema.si_ad_createTimestamp );
700                 if ( !a ) {
701                         attr_merge_one( op->ora_e,
702                                 slap_schema.si_ad_createTimestamp, &timestamp, NULL );
703                 }
704
705                 if ( !gotcsn ) {
706                         attr_merge_one( op->ora_e,
707                                 slap_schema.si_ad_entryCSN, &csn, NULL );
708                 }
709
710                 a = attr_find( op->ora_e->e_attrs,
711                         slap_schema.si_ad_modifiersName );
712                 if ( !a ) {
713                         attr_merge_one( op->ora_e,
714                                 slap_schema.si_ad_modifiersName, &name, &nname );
715                 }
716
717                 a = attr_find( op->ora_e->e_attrs,
718                         slap_schema.si_ad_modifyTimestamp );
719                 if ( !a ) {
720                         attr_merge_one( op->ora_e,
721                                 slap_schema.si_ad_modifyTimestamp, &timestamp, NULL );
722                 }
723         }
724
725         return LDAP_SUCCESS;
726 }