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