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