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