]> 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-2005 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31 #include <ac/socket.h>
32
33 #include "slap.h"
34
35 #ifdef LDAP_SLAPI
36 #include "slapi/slapi.h"
37
38 static void init_add_pblock( Operation *op, struct berval *dn, Entry *e,
39         int manageDSAit );
40 static int call_add_preop_plugins( Operation *op );
41 static void call_add_postop_plugins( Operation *op );
42 #endif /* LDAP_SLAPI */
43
44 int
45 do_add( Operation *op, SlapReply *rs )
46 {
47         BerElement      *ber = op->o_ber;
48         char            *last;
49         struct berval   dn = BER_BVNULL;
50         ber_len_t       len;
51         ber_tag_t       tag;
52         Entry           *e;
53         Modifications   *modlist = NULL;
54         Modifications   **modtail = &modlist;
55         Modifications   tmp;
56         char            textbuf[ SLAP_TEXT_BUFLEN ];
57         size_t          textlen = sizeof( textbuf );
58         int             rc = 0;
59
60         Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
61         /*
62          * Parse the add request.  It looks like this:
63          *
64          *      AddRequest := [APPLICATION 14] SEQUENCE {
65          *              name    DistinguishedName,
66          *              attrs   SEQUENCE OF SEQUENCE {
67          *                      type    AttributeType,
68          *                      values  SET OF AttributeValue
69          *              }
70          *      }
71          */
72
73         /* get the name */
74         if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
75                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
76                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
77                 return SLAPD_DISCONNECT;
78         }
79
80         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
81
82         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
83                 op->o_tmpmemctx );
84
85         if ( rs->sr_err != LDAP_SUCCESS ) {
86                 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
87                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
88                 goto done;
89         }
90
91         ber_dupbv( &e->e_name, &op->o_req_dn );
92         ber_dupbv( &e->e_nname, &op->o_req_ndn );
93
94         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
95
96         /* get the attrs */
97         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
98             tag = ber_next_element( ber, &len, last ) )
99         {
100                 Modifications *mod;
101                 ber_tag_t rtag;
102
103                 tmp.sml_nvalues = NULL;
104
105                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
106
107                 if ( rtag == LBER_ERROR ) {
108                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
109                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
110                         rs->sr_err = SLAPD_DISCONNECT;
111                         goto done;
112                 }
113
114                 if ( tmp.sml_values == NULL ) {
115                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
116                                 tmp.sml_type.bv_val, 0, 0 );
117                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
118                                 "no values for attribute type" );
119                         goto done;
120                 }
121
122                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
123                 mod->sml_op = LDAP_MOD_ADD;
124                 mod->sml_next = NULL;
125                 mod->sml_desc = NULL;
126                 mod->sml_type = tmp.sml_type;
127                 mod->sml_values = tmp.sml_values;
128                 mod->sml_nvalues = NULL;
129
130                 *modtail = mod;
131                 modtail = &mod->sml_next;
132         }
133
134         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
135                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
136                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
137                 rs->sr_err = SLAPD_DISCONNECT;
138                 goto done;
139         }
140
141         if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
142                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
143                 goto done;
144         } 
145
146         if ( modlist == NULL ) {
147                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
148                         "no attributes provided" );
149                 goto done;
150         }
151
152         Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
153             op->o_log_prefix, e->e_name.bv_val, 0, 0, 0 );
154
155         if ( dn_match( &e->e_nname, &slap_empty_bv ) ) {
156                 /* protocolError may be a more appropriate error */
157                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
158                         "root DSE already exists" );
159                 goto done;
160
161         } else if ( dn_match( &e->e_nname, &frontendDB->be_schemandn ) ) {
162                 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
163                         "subschema subentry already exists" );
164                 goto done;
165         }
166
167         rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
168                           textbuf, textlen, NULL );
169
170         if ( rs->sr_err != LDAP_SUCCESS ) {
171                 send_ldap_result( op, rs );
172                 goto done;
173         }
174
175         /* temporary; remove if not invoking backend function */
176         op->ora_e = e;
177         op->ora_modlist = modlist;
178
179         op->o_bd = frontendDB;
180         rc = frontendDB->be_add( op, rs );
181         if ( rc == 0 ) {
182                 e = NULL;
183         }
184
185 done:;
186         slap_graduate_commit_csn( op );
187
188         if ( modlist != NULL ) {
189                 slap_mods_free( modlist );
190         }
191         if ( e != NULL ) {
192                 entry_free( e );
193         }
194         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
195         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
196
197         return rc;
198 }
199
200 int
201 fe_op_add( Operation *op, SlapReply *rs )
202 {
203         int             manageDSAit;
204         Entry           *e = op->ora_e;
205         Modifications   *modlist = op->ora_modlist;
206         Modifications   **modtail = &modlist;
207         int             rc = 0;
208
209         manageDSAit = get_manageDSAit( op );
210
211         /*
212          * We could be serving multiple database backends.  Select the
213          * appropriate one, or send a referral to our "referral server"
214          * if we don't hold it.
215          */
216         op->o_bd = select_backend( &e->e_nname, manageDSAit, 1 );
217         if ( op->o_bd == NULL ) {
218                 rs->sr_ref = referral_rewrite( default_referral,
219                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
220                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
221                 if ( rs->sr_ref ) {
222                         rs->sr_err = LDAP_REFERRAL;
223                         op->o_bd = frontendDB;
224                         send_ldap_result( op, rs );
225                         op->o_bd = NULL;
226
227                         if ( rs->sr_ref != default_referral ) {
228                                 ber_bvarray_free( rs->sr_ref );
229                         }
230                 } else {
231                         op->o_bd = frontendDB;
232                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
233                                 "no global superior knowledge" );
234                         op->o_bd = NULL;
235                 }
236                 goto done;
237         }
238
239         /* check restrictions */
240         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
241                 send_ldap_result( op, rs );
242                 goto done;
243         }
244
245         /* check for referrals */
246         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
247                 goto done;
248         }
249
250 #ifdef LDAP_SLAPI
251         if ( op->o_pb ) init_add_pblock( op, &op->o_req_dn, e, manageDSAit );
252 #endif /* LDAP_SLAPI */
253
254         /*
255          * do the add if 1 && (2 || 3)
256          * 1) there is an add function implemented in this backend;
257          * 2) this backend is master for what it holds;
258          * 3) it's a replica and the dn supplied is the updatedn.
259          */
260         if ( op->o_bd->be_add ) {
261                 /* do the update here */
262                 int repl_user = be_isupdate( op );
263 #ifndef SLAPD_MULTIMASTER
264                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
265 #endif
266                 {
267                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
268                         char            textbuf[ SLAP_TEXT_BUFLEN ];
269                         size_t          textlen = sizeof( textbuf );
270                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
271
272                         if ( !update ) {
273                                 rs->sr_err = slap_mods_no_update_check( modlist,
274                                                 &rs->sr_text,
275                                                 textbuf, textlen );
276
277                                 if ( rs->sr_err != LDAP_SUCCESS ) {
278                                         send_ldap_result( op, rs );
279                                         goto done;
280                                 }
281                         }
282
283                         if ( !repl_user ) {
284                                 /* go to the last mod */
285                                 for ( modtail = &modlist;
286                                                 *modtail != NULL;
287                                                 modtail = &(*modtail)->sml_next )
288                                 {
289                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
290                                         assert( (*modtail)->sml_desc != NULL );
291                                 }
292
293                                 rs->sr_err = slap_mods_opattrs( op, modlist,
294                                                 modtail, &rs->sr_text,
295                                                 textbuf, textlen, 1 );
296                                 if ( rs->sr_err != LDAP_SUCCESS ) {
297                                         send_ldap_result( op, rs );
298                                         goto done;
299                                 }
300                         }
301
302                         rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
303                                 &rs->sr_text, textbuf, textlen );
304                         if ( rs->sr_err != LDAP_SUCCESS ) {
305                                 send_ldap_result( op, rs );
306                                 goto done;
307                         }
308
309 #ifdef LDAP_SLAPI
310                         /*
311                          * Call the preoperation plugin here, because the entry
312                          * will actually contain something.
313                          */
314                         if ( op->o_pb ) {
315                                 rs->sr_err = call_add_preop_plugins( op );
316                                 if ( rs->sr_err != LDAP_SUCCESS ) {
317                                         /* plugin will have sent result */
318                                         goto done;
319                                 }
320                         }
321 #endif /* LDAP_SLAPI */
322
323                         op->ora_e = e;
324 #ifdef SLAPD_MULTIMASTER
325                         if ( !repl_user )
326 #endif
327                         {
328                                 cb.sc_next = op->o_callback;
329                                 op->o_callback = &cb;
330                         }
331                         rc = (op->o_bd->be_add)( op, rs );
332                         if ( rc == 0 ) {
333                                 /* FIXME: be_entry_release_w() should be
334                                  * called by do_add(), so that global
335                                  * overlays on the way back can
336                                  * at least read the entry */
337                                 be_entry_release_w( op, e );
338                                 e = NULL;
339                         }
340                         op->ora_e = NULL;
341
342 #ifndef SLAPD_MULTIMASTER
343                 } else {
344                         BerVarray defref = NULL;
345 #ifdef LDAP_SLAPI
346                         /*
347                          * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
348                          * on replicas (for now, it involves the minimum code intrusion).
349                          */
350                         if ( op->o_pb ) {
351                                 rs->sr_err = call_add_preop_plugins( op );
352                                 if ( rs->sr_err != LDAP_SUCCESS ) {
353                                         /* plugin will have sent result */
354                                         goto done;
355                                 }
356                         }
357 #endif /* LDAP_SLAPI */
358
359                         defref = op->o_bd->be_update_refs
360                                 ? op->o_bd->be_update_refs : default_referral;
361
362                         if ( defref != NULL ) {
363                                 rs->sr_ref = referral_rewrite( defref,
364                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
365                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
366                                 rs->sr_err = LDAP_REFERRAL;
367                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
368                                 send_ldap_result( op, rs );
369
370                                 if ( rs->sr_ref != default_referral ) {
371                                         ber_bvarray_free( rs->sr_ref );
372                                 }
373                         } else {
374                                 send_ldap_error( op, rs,
375                                         LDAP_UNWILLING_TO_PERFORM,
376                                         "shadow context; no update referral" );
377                         }
378 #endif /* SLAPD_MULTIMASTER */
379                 }
380         } else {
381 #ifdef LDAP_SLAPI
382                 if ( op->o_pb ) {
383                         rs->sr_err = call_add_preop_plugins( op );
384                         if ( rs->sr_err != LDAP_SUCCESS ) {
385                                 /* plugin will have sent result */
386                                 goto done;
387                         }
388                 }
389 #endif
390             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
391             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
392                         "operation not supported within namingContext" );
393         }
394
395 #ifdef LDAP_SLAPI
396         if ( op->o_pb ) call_add_postop_plugins( op );
397 #endif /* LDAP_SLAPI */
398
399 done:;
400         return rc;
401 }
402
403 int
404 slap_mods2entry(
405         Modifications *mods,
406         Entry **e,
407         int repl_user,
408         int dup,
409         const char **text,
410         char *textbuf, size_t textlen )
411 {
412         Attribute **tail = &(*e)->e_attrs;
413         assert( *tail == NULL );
414
415         *text = textbuf;
416
417         for( ; mods != NULL; mods = mods->sml_next ) {
418                 Attribute *attr;
419
420                 if ( !repl_user ) {
421                         assert( mods->sml_op == LDAP_MOD_ADD );
422                 }
423                 assert( mods->sml_desc != NULL );
424
425                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
426
427                 if( attr != NULL ) {
428 #define SLURPD_FRIENDLY
429 #ifdef SLURPD_FRIENDLY
430                         ber_len_t i,j;
431
432                         if( !repl_user ) {
433                                 snprintf( textbuf, textlen,
434                                         "attribute '%s' provided more than once",
435                                         mods->sml_desc->ad_cname.bv_val );
436                                 return LDAP_TYPE_OR_VALUE_EXISTS;
437                         }
438
439                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
440                                 /* count them */
441                         }
442                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
443                                 /* count them */
444                         }
445                         j++;    /* NULL */
446                         
447                         attr->a_vals = ch_realloc( attr->a_vals,
448                                 sizeof( struct berval ) * (i+j) );
449
450                         /* should check for duplicates */
451
452                         if ( dup ) {
453                                 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
454                                         ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
455                                 }
456                                 BER_BVZERO( &attr->a_vals[i+j] );       
457                         } else {
458                                 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
459                                         sizeof( struct berval ) * j );
460                                 ch_free( mods->sml_values );
461                                 mods->sml_values = NULL;
462                         }
463
464                         if( mods->sml_nvalues ) {
465                                 attr->a_nvals = ch_realloc( attr->a_nvals,
466                                         sizeof( struct berval ) * (i+j) );
467                                 if ( dup ) {
468                                         for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
469                                                 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
470                                         }
471                                         BER_BVZERO( &attr->a_nvals[i+j] );      
472                                 } else {
473                                         AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
474                                                 sizeof( struct berval ) * j );
475                                         ch_free( mods->sml_nvalues );
476                                         mods->sml_nvalues = NULL;
477                                 }
478                         } else {
479                                 attr->a_nvals = attr->a_vals;
480                         }
481
482                         continue;
483 #else
484                         snprintf( textbuf, textlen,
485                                 "attribute '%s' provided more than once",
486                                 mods->sml_desc->ad_cname.bv_val );
487                         return LDAP_TYPE_OR_VALUE_EXISTS;
488 #endif
489                 }
490
491                 if( mods->sml_values[1].bv_val != NULL ) {
492                         /* check for duplicates */
493                         int             i, j;
494                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
495
496                         /* check if the values we're adding already exist */
497                         if( mr == NULL || !mr->smr_match ) {
498                                 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
499                                         /* test asserted values against themselves */
500                                         for( j = 0; j < i; j++ ) {
501                                                 if ( bvmatch( &mods->sml_values[i],
502                                                         &mods->sml_values[j] ) )
503                                                 {
504                                                         /* value exists already */
505                                                         snprintf( textbuf, textlen,
506                                                                 "%s: value #%d provided more than once",
507                                                                 mods->sml_desc->ad_cname.bv_val, j );
508                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
509                                                 }
510                                         }
511                                 }
512
513                         } else {
514                                 int     rc;
515                                 int match;
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 = 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
548                 attr = ch_calloc( 1, sizeof(Attribute) );
549
550                 /* move ad to attr structure */
551                 attr->a_desc = mods->sml_desc;
552                 if ( !dup ) mods->sml_desc = NULL;
553
554                 /* move values to attr structure */
555                 /*      should check for duplicates */
556                 if ( dup ) { 
557                         int i;
558                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
559                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
560                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
561                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
562                         }
563                         BER_BVZERO( &attr->a_vals[i] );
564                 } else {
565                         attr->a_vals = mods->sml_values;
566                         mods->sml_values = NULL;
567                 }
568
569                 if ( mods->sml_nvalues ) {
570                         if ( dup ) {
571                                 int i;
572                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
573                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
574                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
575                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
576                                 }
577                                 BER_BVZERO( &attr->a_nvals[i] );
578                         } else {
579                                 attr->a_nvals = mods->sml_nvalues;
580                                 mods->sml_nvalues = NULL;
581                         }
582                 } else {
583                         attr->a_nvals = attr->a_vals;
584                 }
585
586                 *tail = attr;
587                 tail = &attr->a_next;
588         }
589
590         *text = NULL;
591
592         return LDAP_SUCCESS;
593 }
594
595 int
596 slap_entry2mods(
597         Entry *e,
598         Modifications **mods,
599         const char **text,
600         char *textbuf, size_t textlen )
601 {
602         Modifications   *modhead = NULL;
603         Modifications   *mod;
604         Modifications   **modtail = &modhead;
605         Attribute               *a_new;
606         AttributeDescription    *a_new_desc;
607         int                             i, count;
608
609         a_new = e->e_attrs;
610
611         while ( a_new != NULL ) {
612                 a_new_desc = a_new->a_desc;
613                 mod = (Modifications *) malloc( sizeof( Modifications ));
614                 
615                 mod->sml_op = LDAP_MOD_REPLACE;
616
617                 mod->sml_type = a_new_desc->ad_cname;
618
619                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
620
621                 mod->sml_values = (struct berval*) malloc(
622                         (count+1) * sizeof( struct berval) );
623
624                 /* see slap_mods_check() comments...
625                  * if a_vals == a_nvals, there is no normalizer.
626                  * in this case, mod->sml_nvalues must be left NULL.
627                  */
628                 if ( a_new->a_vals != a_new->a_nvals ) {
629                         mod->sml_nvalues = (struct berval*) malloc(
630                                 (count+1) * sizeof( struct berval) );
631                 } else {
632                         mod->sml_nvalues = NULL;
633                 }
634
635                 for ( i = 0; i < count; i++ ) {
636                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
637                         if ( mod->sml_nvalues ) {
638                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
639                         } 
640                 }
641
642                 mod->sml_values[count].bv_val = NULL; 
643                 mod->sml_values[count].bv_len = 0; 
644
645                 if ( mod->sml_nvalues ) {
646                         mod->sml_nvalues[count].bv_val = NULL; 
647                         mod->sml_nvalues[count].bv_len = 0; 
648                 }
649
650                 mod->sml_desc = a_new_desc;
651                 mod->sml_next =NULL;
652                 *modtail = mod;
653                 modtail = &mod->sml_next;
654                 a_new = a_new->a_next; 
655         }
656
657         *mods = modhead;
658
659         return LDAP_SUCCESS;
660 }
661
662 #ifdef LDAP_SLAPI
663 static void init_add_pblock( Operation *op,
664         struct berval *dn, Entry *e, int manageDSAit )
665 {
666         slapi_int_pblock_set_operation( op->o_pb, op );
667         slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
668         slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
669         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
670 }
671
672 static int call_add_preop_plugins( Operation *op )
673 {
674         int rc;
675
676         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
677         if ( rc < 0 ) {
678                 /*
679                  * A preoperation plugin failure will abort the
680                  * entire operation.
681                  */
682                 Debug(LDAP_DEBUG_TRACE,
683                         "do_add: add preoperation plugin failed.\n",
684                         0, 0, 0);
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                 Debug(LDAP_DEBUG_TRACE,
705                         "do_add: add postoperation plugin failed\n",
706                         0, 0, 0);
707         }
708 }
709 #endif /* LDAP_SLAPI */