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