]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
Sync with HEAD
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2003 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.h"
38 static void initAddPlugin( Operation *op,
39         struct berval *dn, Entry *e, int manageDSAit );
40 static int doPreAddPluginFNs( Operation *op );
41 static void doPostAddPluginFNs( 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 ) initAddPlugin( 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
265                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
266                                                                                   textbuf, textlen, NULL );
267
268                         if( rs->sr_err != LDAP_SUCCESS ) {
269                                 send_ldap_result( op, rs );
270                                 goto done;
271                         }
272
273                         if ( !repl_user ) {
274                                 for( modtail = &modlist;
275                                         *modtail != NULL;
276                                         modtail = &(*modtail)->sml_next )
277                                 {
278                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
279                                         assert( (*modtail)->sml_desc != NULL );
280                                 }
281                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
282                                         &rs->sr_text, textbuf, textlen );
283                                 if( rs->sr_err != LDAP_SUCCESS ) {
284                                         send_ldap_result( op, rs );
285                                         goto done;
286                                 }
287                         }
288
289                         rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
290                                                                         &rs->sr_text, textbuf, textlen );
291                         if( rs->sr_err != LDAP_SUCCESS ) {
292                                 send_ldap_result( op, rs );
293                                 goto done;
294                         }
295
296 #ifdef LDAP_SLAPI
297                         /*
298                          * Call the preoperation plugin here, because the entry
299                          * will actually contain something.
300                          */
301                         if ( op->o_pb ) {
302                                 rs->sr_err = doPreAddPluginFNs( op );
303                                 if ( rs->sr_err != LDAP_SUCCESS ) {
304                                         /* plugin will have sent result */
305                                         goto done;
306                                 }
307                         }
308 #endif /* LDAP_SLAPI */
309
310                         op->ora_e = e;
311                         if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
312 #ifdef SLAPD_MULTIMASTER
313                                 if ( !repl_user )
314 #endif
315                                 {
316                                         replog( op );
317                                 }
318                                 be_entry_release_w( op, e );
319                                 e = NULL;
320                         }
321
322 #ifndef SLAPD_MULTIMASTER
323                 } else {
324                         BerVarray defref = NULL;
325 #ifdef LDAP_SLAPI
326                         /*
327                          * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
328                          * on replicas (for now, it involves the minimum code intrusion).
329                          */
330                         if ( op->o_pb ) {
331                                 rs->sr_err = doPreAddPluginFNs( op );
332                                 if ( rs->sr_err != LDAP_SUCCESS ) {
333                                         /* plugin will have sent result */
334                                         goto done;
335                                 }
336                         }
337 #endif /* LDAP_SLAPI */
338
339                         if ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
340                                 syncinfo_t *si;
341                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
342                                         struct berval tmpbv;
343                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
344                                         ber_bvarray_add( &defref, &tmpbv );
345                                 }
346                         } else {
347                                 defref = op->o_bd->be_update_refs
348                                         ? op->o_bd->be_update_refs : default_referral;
349                         }
350
351                         if ( defref != NULL ) {
352                                 rs->sr_ref = referral_rewrite( defref,
353                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
354                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
355                                 rs->sr_err = LDAP_REFERRAL;
356                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
357                                 send_ldap_result( op, rs );
358
359                                 if ( rs->sr_ref != default_referral ) ber_bvarray_free( rs->sr_ref );
360                         } else {
361                                 send_ldap_error( op, rs,
362                                                 LDAP_UNWILLING_TO_PERFORM,
363                                                 "referral missing" );
364                         }
365 #endif /* SLAPD_MULTIMASTER */
366                 }
367         } else {
368 #ifdef LDAP_SLAPI
369                 if ( op->o_pb ) {
370                         rs->sr_err = doPreAddPluginFNs( op );
371                         if ( rs->sr_err != LDAP_SUCCESS ) {
372                                 /* plugin will have sent result */
373                                 goto done;
374                         }
375                 }
376 #endif
377 #ifdef NEW_LOGGING
378             LDAP_LOG( OPERATION, INFO, 
379                        "do_add: conn %d  no backend support\n", op->o_connid, 0, 0 );
380 #else
381             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
382 #endif
383             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
384                         "operation not supported within namingContext" );
385         }
386
387 #ifdef LDAP_SLAPI
388         if ( op->o_pb ) doPostAddPluginFNs( op );
389 #endif /* LDAP_SLAPI */
390
391 done:
392
393         slap_graduate_commit_csn( op );
394
395         if( modlist != NULL ) {
396                 slap_mods_free( modlist );
397         }
398         if( e != NULL ) {
399                 entry_free( e );
400         }
401         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
402         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
403
404         return rs->sr_err;
405 }
406
407 int
408 slap_mods2entry(
409         Modifications *mods,
410         Entry **e,
411         int repl_user,
412         int dup,
413         const char **text,
414         char *textbuf, size_t textlen )
415 {
416         Attribute **tail = &(*e)->e_attrs;
417         assert( *tail == NULL );
418
419         *text = textbuf;
420
421         for( ; mods != NULL; mods = mods->sml_next ) {
422                 Attribute *attr;
423
424                 if ( !repl_user ) {
425                         assert( mods->sml_op == LDAP_MOD_ADD );
426                 }
427                 assert( mods->sml_desc != NULL );
428
429                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
430
431                 if( attr != NULL ) {
432 #define SLURPD_FRIENDLY
433 #ifdef SLURPD_FRIENDLY
434                         ber_len_t i,j;
435
436                         if( !repl_user ) {
437                                 snprintf( textbuf, textlen,
438                                         "attribute '%s' provided more than once",
439                                         mods->sml_desc->ad_cname.bv_val );
440                                 return LDAP_TYPE_OR_VALUE_EXISTS;
441                         }
442
443                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
444                                 /* count them */
445                         }
446                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
447                                 /* count them */
448                         }
449                         j++;    /* NULL */
450                         
451                         attr->a_vals = ch_realloc( attr->a_vals,
452                                 sizeof( struct berval ) * (i+j) );
453
454                         /* should check for duplicates */
455
456                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
457                                 sizeof( struct berval ) * j );
458
459                         /* trim the mods array */
460                         ch_free( mods->sml_values );
461                         mods->sml_values = NULL;
462
463                         if( mods->sml_nvalues ) {
464                                 attr->a_nvals = ch_realloc( attr->a_nvals,
465                                         sizeof( struct berval ) * (i+j) );
466
467                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
468                                         sizeof( struct berval ) * j );
469
470                                 /* trim the mods array */
471                                 ch_free( mods->sml_nvalues );
472                                 mods->sml_nvalues = NULL;
473
474                         } else {
475                                 attr->a_nvals = attr->a_vals;
476                         }
477
478                         continue;
479 #else
480                         snprintf( textbuf, textlen,
481                                 "attribute '%s' provided more than once",
482                                 mods->sml_desc->ad_cname.bv_val );
483                         return LDAP_TYPE_OR_VALUE_EXISTS;
484 #endif
485                 }
486
487                 if( mods->sml_values[1].bv_val != NULL ) {
488                         /* check for duplicates */
489                         int             i, j;
490                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
491
492                         /* check if the values we're adding already exist */
493                         if( mr == NULL || !mr->smr_match ) {
494                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
495                                         /* test asserted values against themselves */
496                                         for( j = 0; j < i; j++ ) {
497                                                 if ( bvmatch( &mods->sml_bvalues[i],
498                                                         &mods->sml_bvalues[j] ) ) {
499                                                         /* value exists already */
500                                                         snprintf( textbuf, textlen,
501                                                                 "%s: value #%d provided more than once",
502                                                                 mods->sml_desc->ad_cname.bv_val, j );
503                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
504                                                 }
505                                         }
506                                 }
507
508                         } else {
509                                 int             rc = LDAP_SUCCESS;
510                                 int match;
511
512                                 for ( i = 0; mods->sml_values[i].bv_val != NULL; i++ ) {
513                                         /* test asserted values against themselves */
514                                         for( j = 0; j < i; j++ ) {
515                                                 rc = value_match( &match, mods->sml_desc, mr,
516                                                         SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
517                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
518                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
519                                                         mods->sml_nvalues
520                                                                 ? &mods->sml_nvalues[i]
521                                                                 : &mods->sml_values[i],
522                                                         mods->sml_nvalues
523                                                                 ? &mods->sml_nvalues[j]
524                                                                 : &mods->sml_values[j],
525                                                         text );
526                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
527                                                         /* value exists already */
528                                                         snprintf( textbuf, textlen,
529                                                                 "%s: value #%d provided more than once",
530                                                                 mods->sml_desc->ad_cname.bv_val, j );
531                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
532                                                 }
533                                         }
534                                 }
535                                 if ( rc != LDAP_SUCCESS ) {
536                                         return rc;
537                                 }
538                         }
539                 }
540
541                 attr = ch_calloc( 1, sizeof(Attribute) );
542
543                 /* move ad to attr structure */
544                 attr->a_desc = mods->sml_desc;
545                 if ( !dup )
546                         mods->sml_desc = NULL;
547
548                 /* move values to attr structure */
549                 /*      should check for duplicates */
550                 if ( dup ) { 
551                         int i;
552                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) ;
553                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
554                         for ( i = 0; mods->sml_values[i].bv_val; i++ )
555                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
556                         attr->a_vals[i].bv_len = 0;
557                         attr->a_vals[i].bv_val = NULL;
558                 } else {
559                         attr->a_vals = mods->sml_values;
560                         mods->sml_values = NULL;
561                 }
562
563                 if ( mods->sml_nvalues ) {
564                         if ( dup ) {
565                                 int i;
566                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) ;
567                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
568                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ )
569                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
570                                 attr->a_nvals[i].bv_len = 0;
571                                 attr->a_nvals[i].bv_val = NULL;
572                         } else {
573                                 attr->a_nvals = mods->sml_nvalues;
574                                 mods->sml_nvalues = NULL;
575                         }
576                 } else {
577                         attr->a_nvals = attr->a_vals;
578                 }
579
580                 *tail = attr;
581                 tail = &attr->a_next;
582         }
583
584         return LDAP_SUCCESS;
585 }
586
587 int
588 slap_entry2mods(
589         Entry *e,
590         Modifications **mods,
591         const char **text,
592         char *textbuf, size_t textlen )
593 {
594         Modifications   *modhead = NULL;
595         Modifications   *mod;
596         Modifications   **modtail = &modhead;
597         Attribute               *a_new;
598         AttributeDescription    *a_new_desc;
599         int                             i, count;
600
601         a_new = e->e_attrs;
602
603         while ( a_new != NULL ) {
604                 a_new_desc = a_new->a_desc;
605                 mod = (Modifications *) malloc( sizeof( Modifications ));
606                 
607                 mod->sml_op = LDAP_MOD_REPLACE;
608
609                 mod->sml_type = a_new_desc->ad_cname;
610
611                 for ( count = 0; a_new->a_vals[count].bv_val; count++ );
612
613                 mod->sml_bvalues = (struct berval*) malloc(
614                                 (count+1) * sizeof( struct berval) );
615
616                 /* see slap_mods_check() comments...
617                  * if a_vals == a_nvals, there is no normalizer.
618                  * in this case, mod->sml_nvalues must be left NULL.
619                  */
620                 if ( a_new->a_vals != a_new->a_nvals ) {
621                         mod->sml_nvalues = (struct berval*) malloc(
622                                 (count+1) * sizeof( struct berval) );
623                 } else {
624                         mod->sml_nvalues = NULL;
625                 }
626
627                 for ( i = 0; i < count; i++ ) {
628                         ber_dupbv(mod->sml_bvalues+i, a_new->a_vals+i); 
629                         if ( mod->sml_nvalues ) {
630                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_vals+i ); 
631                         } 
632                 }
633
634                 mod->sml_bvalues[count].bv_val = 0; 
635                 mod->sml_bvalues[count].bv_len = 0; 
636
637                 if ( mod->sml_nvalues ) {
638                         mod->sml_nvalues[count].bv_val = 0; 
639                         mod->sml_nvalues[count].bv_len = 0; 
640                 }
641
642                 mod->sml_desc = a_new_desc;
643                 mod->sml_next =NULL;
644                 *modtail = mod;
645                 modtail = &mod->sml_next;
646                 a_new = a_new->a_next; 
647         }
648
649         *mods = modhead;
650
651         return LDAP_SUCCESS;
652 }
653
654 #ifdef LDAP_SLAPI
655 static void initAddPlugin( Operation *op,
656         struct berval *dn, Entry *e, int manageDSAit )
657 {
658         slapi_x_pblock_set_operation( op->o_pb, op );
659         slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
660         slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
661         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
662 }
663
664 static int doPreAddPluginFNs( Operation *op )
665 {
666         int rc;
667
668         rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
669         if ( rc < 0 ) {
670                 /*
671                  * A preoperation plugin failure will abort the
672                  * entire operation.
673                  */
674 #ifdef NEW_LOGGING
675                 LDAP_LOG( OPERATION, INFO,
676                         "do_add: add preoperation plugin failed\n",
677                         0, 0, 0);
678 #else
679                 Debug(LDAP_DEBUG_TRACE,
680                         "do_add: add preoperation plugin failed.\n",
681                         0, 0, 0);
682 #endif
683
684                 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
685                         (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
686                 {
687                         rc = LDAP_OTHER;
688                 }
689         } else {
690                 rc = LDAP_SUCCESS;
691         }
692
693         return rc;
694 }
695
696 static void doPostAddPluginFNs( Operation *op )
697 {
698         int rc;
699
700         rc = doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
701         if ( rc < 0 ) {
702 #ifdef NEW_LOGGING
703                 LDAP_LOG( OPERATION, INFO,
704                         "do_add: add postoperation plugin failed\n",
705                         0, 0, 0);
706 #else
707                 Debug(LDAP_DEBUG_TRACE,
708                         "do_add: add postoperation plugin failed\n",
709                         0, 0, 0);
710 #endif
711         }
712 }
713 #endif /* LDAP_SLAPI */