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