]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
updatedn fix for syncrepl
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29 #include <ac/string.h>
30 #include <ac/time.h>
31 #include <ac/socket.h>
32
33 #include "ldap_pvt.h"
34 #include "slap.h"
35
36 #ifdef LDAP_SLAPI
37 #include "slapi/slapi.h"
38
39 static void init_add_pblock( Operation *op, struct berval *dn, Entry *e, int manageDSAit );
40 static int call_add_preop_plugins( Operation *op );
41 static void call_add_postop_plugins( Operation *op );
42 #endif /* LDAP_SLAPI */
43
44 int
45 do_add( Operation *op, SlapReply *rs )
46 {
47         BerElement      *ber = op->o_ber;
48         char            *last;
49         struct berval dn = 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         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 ) {
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                                 "no global superior knowledge" );
226                 }
227                 goto done;
228         }
229
230         /* check restrictions */
231         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
232                 send_ldap_result( op, rs );
233                 goto done;
234         }
235
236         /* check for referrals */
237         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
238                 goto done;
239         }
240
241 #ifdef LDAP_SLAPI
242         if ( op->o_pb ) init_add_pblock( op, &dn, e, manageDSAit );
243 #endif /* LDAP_SLAPI */
244
245         /*
246          * do the add if 1 && (2 || 3)
247          * 1) there is an add function implemented in this backend;
248          * 2) this backend is master for what it holds;
249          * 3) it's a replica and the dn supplied is the updatedn.
250          */
251         if ( op->o_bd->be_add ) {
252                 /* do the update here */
253                 int repl_user = be_isupdate( op );
254 #ifndef SLAPD_MULTIMASTER
255                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
256 #endif
257                 {
258                         int update = op->o_bd->be_update_ndn.bv_len;
259                         char textbuf[SLAP_TEXT_BUFLEN];
260                         size_t textlen = sizeof textbuf;
261                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
262
263                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
264                                   textbuf, textlen, NULL );
265
266                         if( rs->sr_err != LDAP_SUCCESS ) {
267                                 send_ldap_result( op, rs );
268                                 goto done;
269                         }
270
271                         if ( !repl_user ) {
272                                 for( modtail = &modlist;
273                                         *modtail != NULL;
274                                         modtail = &(*modtail)->sml_next )
275                                 {
276                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
277                                         assert( (*modtail)->sml_desc != NULL );
278                                 }
279                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
280                                         &rs->sr_text, textbuf, textlen );
281                                 if( rs->sr_err != LDAP_SUCCESS ) {
282                                         send_ldap_result( op, rs );
283                                         goto done;
284                                 }
285                         }
286
287                         rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
288                                 &rs->sr_text, textbuf, textlen );
289                         if( rs->sr_err != LDAP_SUCCESS ) {
290                                 send_ldap_result( op, rs );
291                                 goto done;
292                         }
293
294 #ifdef LDAP_SLAPI
295                         /*
296                          * Call the preoperation plugin here, because the entry
297                          * will actually contain something.
298                          */
299                         if ( op->o_pb ) {
300                                 rs->sr_err = call_add_preop_plugins( op );
301                                 if ( rs->sr_err != LDAP_SUCCESS ) {
302                                         /* plugin will have sent result */
303                                         goto done;
304                                 }
305                         }
306 #endif /* LDAP_SLAPI */
307
308                         op->ora_e = e;
309 #ifdef SLAPD_MULTIMASTER
310                         if ( !repl_user )
311 #endif
312                         {
313                                 cb.sc_next = op->o_callback;
314                                 op->o_callback = &cb;
315                         }
316                         if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
317                                 be_entry_release_w( op, e );
318                                 e = NULL;
319                         }
320
321 #ifndef SLAPD_MULTIMASTER
322                 } else {
323                         BerVarray defref = NULL;
324 #ifdef LDAP_SLAPI
325                         /*
326                          * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
327                          * on replicas (for now, it involves the minimum code intrusion).
328                          */
329                         if ( op->o_pb ) {
330                                 rs->sr_err = call_add_preop_plugins( op );
331                                 if ( rs->sr_err != LDAP_SUCCESS ) {
332                                         /* plugin will have sent result */
333                                         goto done;
334                                 }
335                         }
336 #endif /* LDAP_SLAPI */
337
338                         defref = op->o_bd->be_update_refs
339                                 ? op->o_bd->be_update_refs : default_referral;
340
341                         if ( defref != NULL ) {
342                                 rs->sr_ref = referral_rewrite( defref,
343                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
344                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
345                                 rs->sr_err = LDAP_REFERRAL;
346                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
347                                 send_ldap_result( op, rs );
348
349                                 if ( rs->sr_ref != default_referral ) {
350                                         ber_bvarray_free( rs->sr_ref );
351                                 }
352                         } else {
353                                 send_ldap_error( op, rs,
354                                         LDAP_UNWILLING_TO_PERFORM,
355                                         "shadow context; no update referral" );
356                         }
357 #endif /* SLAPD_MULTIMASTER */
358                 }
359         } else {
360 #ifdef LDAP_SLAPI
361                 if ( op->o_pb ) {
362                         rs->sr_err = call_add_preop_plugins( op );
363                         if ( rs->sr_err != LDAP_SUCCESS ) {
364                                 /* plugin will have sent result */
365                                 goto done;
366                         }
367                 }
368 #endif
369 #ifdef NEW_LOGGING
370             LDAP_LOG( OPERATION, INFO, 
371                        "do_add: conn %d  no backend support\n", op->o_connid, 0, 0 );
372 #else
373             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
374 #endif
375             send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
376                         "operation not supported within namingContext" );
377         }
378
379 #ifdef LDAP_SLAPI
380         if ( op->o_pb ) call_add_postop_plugins( op );
381 #endif /* LDAP_SLAPI */
382
383 done:
384
385         slap_graduate_commit_csn( op );
386
387         if( modlist != NULL ) {
388                 slap_mods_free( modlist );
389         }
390         if( e != NULL ) {
391                 entry_free( e );
392         }
393         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
394         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
395
396         return rs->sr_err;
397 }
398
399 int
400 slap_mods2entry(
401         Modifications *mods,
402         Entry **e,
403         int repl_user,
404         int dup,
405         const char **text,
406         char *textbuf, size_t textlen )
407 {
408         Attribute **tail = &(*e)->e_attrs;
409         assert( *tail == NULL );
410
411         *text = textbuf;
412
413         for( ; mods != NULL; mods = mods->sml_next ) {
414                 Attribute *attr;
415
416                 if ( !repl_user ) {
417                         assert( mods->sml_op == LDAP_MOD_ADD );
418                 }
419                 assert( mods->sml_desc != NULL );
420
421                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
422
423                 if( attr != NULL ) {
424 #define SLURPD_FRIENDLY
425 #ifdef SLURPD_FRIENDLY
426                         ber_len_t i,j;
427
428                         if( !repl_user ) {
429                                 snprintf( textbuf, textlen,
430                                         "attribute '%s' provided more than once",
431                                         mods->sml_desc->ad_cname.bv_val );
432                                 return LDAP_TYPE_OR_VALUE_EXISTS;
433                         }
434
435                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
436                                 /* count them */
437                         }
438                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
439                                 /* count them */
440                         }
441                         j++;    /* NULL */
442                         
443                         attr->a_vals = ch_realloc( attr->a_vals,
444                                 sizeof( struct berval ) * (i+j) );
445
446                         /* should check for duplicates */
447
448                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
449                                 sizeof( struct berval ) * j );
450
451                         /* trim the mods array */
452                         ch_free( mods->sml_values );
453                         mods->sml_values = NULL;
454
455                         if( mods->sml_nvalues ) {
456                                 attr->a_nvals = ch_realloc( attr->a_nvals,
457                                         sizeof( struct berval ) * (i+j) );
458
459                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
460                                         sizeof( struct berval ) * j );
461
462                                 /* trim the mods array */
463                                 ch_free( mods->sml_nvalues );
464                                 mods->sml_nvalues = NULL;
465
466                         } else {
467                                 attr->a_nvals = attr->a_vals;
468                         }
469
470                         continue;
471 #else
472                         snprintf( textbuf, textlen,
473                                 "attribute '%s' provided more than once",
474                                 mods->sml_desc->ad_cname.bv_val );
475                         return LDAP_TYPE_OR_VALUE_EXISTS;
476 #endif
477                 }
478
479                 if( mods->sml_values[1].bv_val != NULL ) {
480                         /* check for duplicates */
481                         int             i, j;
482                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
483
484                         /* check if the values we're adding already exist */
485                         if( mr == NULL || !mr->smr_match ) {
486                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
487                                         /* test asserted values against themselves */
488                                         for( j = 0; j < i; j++ ) {
489                                                 if ( bvmatch( &mods->sml_bvalues[i],
490                                                         &mods->sml_bvalues[j] ) ) {
491                                                         /* value exists already */
492                                                         snprintf( textbuf, textlen,
493                                                                 "%s: value #%d provided more than once",
494                                                                 mods->sml_desc->ad_cname.bv_val, j );
495                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
496                                                 }
497                                         }
498                                 }
499
500                         } else {
501                                 int             rc = LDAP_SUCCESS;
502                                 int match;
503
504                                 for ( i = 0; mods->sml_values[i].bv_val != NULL; i++ ) {
505                                         /* test asserted values against themselves */
506                                         for( j = 0; j < i; j++ ) {
507                                                 rc = value_match( &match, mods->sml_desc, mr,
508                                                         SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
509                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
510                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
511                                                         mods->sml_nvalues
512                                                                 ? &mods->sml_nvalues[i]
513                                                                 : &mods->sml_values[i],
514                                                         mods->sml_nvalues
515                                                                 ? &mods->sml_nvalues[j]
516                                                                 : &mods->sml_values[j],
517                                                         text );
518                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
519                                                         /* value exists already */
520                                                         snprintf( textbuf, textlen,
521                                                                 "%s: value #%d provided more than once",
522                                                                 mods->sml_desc->ad_cname.bv_val, j );
523                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
524                                                 }
525                                         }
526                                 }
527                                 if ( rc != LDAP_SUCCESS ) {
528                                         return rc;
529                                 }
530                         }
531                 }
532
533                 attr = ch_calloc( 1, sizeof(Attribute) );
534
535                 /* move ad to attr structure */
536                 attr->a_desc = mods->sml_desc;
537                 if ( !dup )
538                         mods->sml_desc = NULL;
539
540                 /* move values to attr structure */
541                 /*      should check for duplicates */
542                 if ( dup ) { 
543                         int i;
544                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) ;
545                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
546                         for ( i = 0; mods->sml_values[i].bv_val; i++ )
547                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
548                         attr->a_vals[i].bv_len = 0;
549                         attr->a_vals[i].bv_val = NULL;
550                 } else {
551                         attr->a_vals = mods->sml_values;
552                         mods->sml_values = NULL;
553                 }
554
555                 if ( mods->sml_nvalues ) {
556                         if ( dup ) {
557                                 int i;
558                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) ;
559                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
560                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ )
561                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
562                                 attr->a_nvals[i].bv_len = 0;
563                                 attr->a_nvals[i].bv_val = NULL;
564                         } else {
565                                 attr->a_nvals = mods->sml_nvalues;
566                                 mods->sml_nvalues = NULL;
567                         }
568                 } else {
569                         attr->a_nvals = attr->a_vals;
570                 }
571
572                 *tail = attr;
573                 tail = &attr->a_next;
574         }
575
576         return LDAP_SUCCESS;
577 }
578
579 int
580 slap_entry2mods(
581         Entry *e,
582         Modifications **mods,
583         const char **text,
584         char *textbuf, size_t textlen )
585 {
586         Modifications   *modhead = NULL;
587         Modifications   *mod;
588         Modifications   **modtail = &modhead;
589         Attribute               *a_new;
590         AttributeDescription    *a_new_desc;
591         int                             i, count;
592
593         a_new = e->e_attrs;
594
595         while ( a_new != NULL ) {
596                 a_new_desc = a_new->a_desc;
597                 mod = (Modifications *) malloc( sizeof( Modifications ));
598                 
599                 mod->sml_op = LDAP_MOD_REPLACE;
600
601                 mod->sml_type = a_new_desc->ad_cname;
602
603                 for ( count = 0; a_new->a_vals[count].bv_val; count++ );
604
605                 mod->sml_bvalues = (struct berval*) malloc(
606                                 (count+1) * sizeof( struct berval) );
607
608                 /* see slap_mods_check() comments...
609                  * if a_vals == a_nvals, there is no normalizer.
610                  * in this case, mod->sml_nvalues must be left NULL.
611                  */
612                 if ( a_new->a_vals != a_new->a_nvals ) {
613                         mod->sml_nvalues = (struct berval*) malloc(
614                                 (count+1) * sizeof( struct berval) );
615                 } else {
616                         mod->sml_nvalues = NULL;
617                 }
618
619                 for ( i = 0; i < count; i++ ) {
620                         ber_dupbv(mod->sml_bvalues+i, a_new->a_vals+i); 
621                         if ( mod->sml_nvalues ) {
622                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_vals+i ); 
623                         } 
624                 }
625
626                 mod->sml_bvalues[count].bv_val = 0; 
627                 mod->sml_bvalues[count].bv_len = 0; 
628
629                 if ( mod->sml_nvalues ) {
630                         mod->sml_nvalues[count].bv_val = 0; 
631                         mod->sml_nvalues[count].bv_len = 0; 
632                 }
633
634                 mod->sml_desc = a_new_desc;
635                 mod->sml_next =NULL;
636                 *modtail = mod;
637                 modtail = &mod->sml_next;
638                 a_new = a_new->a_next; 
639         }
640
641         *mods = modhead;
642
643         return LDAP_SUCCESS;
644 }
645
646 #ifdef LDAP_SLAPI
647 static void init_add_pblock( Operation *op,
648         struct berval *dn, Entry *e, int manageDSAit )
649 {
650         slapi_int_pblock_set_operation( op->o_pb, op );
651         slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
652         slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
653         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
654 }
655
656 static int call_add_preop_plugins( Operation *op )
657 {
658         int rc;
659
660         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
661         if ( rc < 0 ) {
662                 /*
663                  * A preoperation plugin failure will abort the
664                  * entire operation.
665                  */
666 #ifdef NEW_LOGGING
667                 LDAP_LOG( OPERATION, INFO,
668                         "do_add: add preoperation plugin failed\n",
669                         0, 0, 0);
670 #else
671                 Debug(LDAP_DEBUG_TRACE,
672                         "do_add: add preoperation plugin failed.\n",
673                         0, 0, 0);
674 #endif
675
676                 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
677                         (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
678                 {
679                         rc = LDAP_OTHER;
680                 }
681         } else {
682                 rc = LDAP_SUCCESS;
683         }
684
685         return rc;
686 }
687
688 static void call_add_postop_plugins( Operation *op )
689 {
690         int rc;
691
692         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
693         if ( rc < 0 ) {
694 #ifdef NEW_LOGGING
695                 LDAP_LOG( OPERATION, INFO,
696                         "do_add: add postoperation plugin failed\n",
697                         0, 0, 0);
698 #else
699                 Debug(LDAP_DEBUG_TRACE,
700                         "do_add: add postoperation plugin failed\n",
701                         0, 0, 0);
702 #endif
703         }
704 }
705 #endif /* LDAP_SLAPI */