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