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