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