]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
5154acfbc990df1017b14d8fa53d491efe844197
[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                 /* FIXME: temporary? */
181                 assert( op->ora_e != NULL );
182                 assert( op->o_private != NULL );
183                 
184                 if ( op->ora_e != NULL ) {
185                         BackendDB       *bd = op->o_bd;
186
187                         op->o_bd = (BackendDB *)op->o_private;
188                         op->o_private = NULL;
189
190                         be_entry_release_w( op, op->ora_e );
191
192                         op->ora_e = NULL;
193                         op->o_bd = bd;
194                         op->o_private = NULL;
195                 }
196         }
197
198 done:;
199         slap_graduate_commit_csn( op );
200
201         if ( modlist != NULL ) {
202                 slap_mods_free( modlist );
203         }
204         if ( op->ora_e != NULL ) {
205                 entry_free( op->ora_e );
206         }
207         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
208         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
209
210         return rc;
211 }
212
213 int
214 fe_op_add( Operation *op, SlapReply *rs )
215 {
216         int             manageDSAit;
217         Modifications   *modlist = op->ora_modlist;
218         Modifications   **modtail = &modlist;
219         int             rc = 0;
220         BackendDB *op_be;
221
222         manageDSAit = get_manageDSAit( op );
223
224         /*
225          * We could be serving multiple database backends.  Select the
226          * appropriate one, or send a referral to our "referral server"
227          * if we don't hold it.
228          */
229         op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
230         if ( op->o_bd == NULL ) {
231                 rs->sr_ref = referral_rewrite( default_referral,
232                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
233                 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
234                 if ( rs->sr_ref ) {
235                         rs->sr_err = LDAP_REFERRAL;
236                         op->o_bd = frontendDB;
237                         send_ldap_result( op, rs );
238                         op->o_bd = NULL;
239
240                         if ( rs->sr_ref != default_referral ) {
241                                 ber_bvarray_free( rs->sr_ref );
242                         }
243                 } else {
244                         op->o_bd = frontendDB;
245                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
246                                 "no global superior knowledge" );
247                         op->o_bd = NULL;
248                 }
249                 goto done;
250         }
251
252         /* If we've got a glued backend, check the real backend */
253         op_be = op->o_bd;
254         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
255                 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
256         }
257
258         /* check restrictions */
259         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
260                 send_ldap_result( op, rs );
261                 goto done;
262         }
263
264         /* check for referrals */
265         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
266                 goto done;
267         }
268
269 #ifdef LDAP_SLAPI
270         if ( op->o_pb ) init_add_pblock( op, &op->o_req_dn, op->ora_e, manageDSAit );
271 #endif /* LDAP_SLAPI */
272
273         /*
274          * do the add if 1 && (2 || 3)
275          * 1) there is an add function implemented in this backend;
276          * 2) this backend is master for what it holds;
277          * 3) it's a replica and the dn supplied is the updatedn.
278          */
279         if ( op->o_bd->be_add ) {
280                 /* do the update here */
281                 int repl_user = be_isupdate( op );
282 #ifndef SLAPD_MULTIMASTER
283                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
284 #endif
285                 {
286                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
287                         char            textbuf[ SLAP_TEXT_BUFLEN ];
288                         size_t          textlen = sizeof( textbuf );
289                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
290
291                         op->o_bd = op_be;
292
293                         if ( !update ) {
294                                 rs->sr_err = slap_mods_no_update_check( modlist,
295                                                 &rs->sr_text,
296                                                 textbuf, textlen );
297
298                                 if ( rs->sr_err != LDAP_SUCCESS ) {
299                                         send_ldap_result( op, rs );
300                                         goto done;
301                                 }
302                         }
303
304                         if ( !repl_user ) {
305                                 /* go to the last mod */
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
314                                 rs->sr_err = slap_mods_opattrs( op, modlist,
315                                                 modtail, &rs->sr_text,
316                                                 textbuf, textlen, 1 );
317                                 if ( rs->sr_err != LDAP_SUCCESS ) {
318                                         send_ldap_result( op, rs );
319                                         goto done;
320                                 }
321                         }
322
323                         rs->sr_err = slap_mods2entry( modlist, &op->ora_e,
324                                 repl_user, 0, &rs->sr_text, textbuf, textlen );
325                         if ( rs->sr_err != LDAP_SUCCESS ) {
326                                 send_ldap_result( op, rs );
327                                 goto done;
328                         }
329
330 #ifdef LDAP_SLAPI
331                         /*
332                          * Call the preoperation plugin here, because the entry
333                          * will actually contain something.
334                          */
335                         if ( op->o_pb ) {
336                                 rs->sr_err = call_add_preop_plugins( op );
337                                 if ( rs->sr_err != LDAP_SUCCESS ) {
338                                         /* plugin will have sent result */
339                                         goto done;
340                                 }
341                         }
342 #endif /* LDAP_SLAPI */
343
344 #ifdef SLAPD_MULTIMASTER
345                         if ( !repl_user )
346 #endif
347                         {
348                                 cb.sc_next = op->o_callback;
349                                 op->o_callback = &cb;
350                         }
351                         rc = op->o_bd->be_add( op, rs );
352                         if ( rc == LDAP_SUCCESS ) {
353                                 /* NOTE: be_entry_release_w() is
354                                  * called by do_add(), so that global
355                                  * overlays on the way back can
356                                  * at least read the entry */
357                                 op->o_private = op->o_bd;
358                         }
359
360 #ifndef SLAPD_MULTIMASTER
361                 } else {
362                         BerVarray defref = NULL;
363 #ifdef LDAP_SLAPI
364                         /*
365                          * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
366                          * on replicas (for now, it involves the minimum code intrusion).
367                          */
368                         if ( op->o_pb ) {
369                                 rs->sr_err = call_add_preop_plugins( op );
370                                 if ( rs->sr_err != LDAP_SUCCESS ) {
371                                         /* plugin will have sent result */
372                                         goto done;
373                                 }
374                         }
375 #endif /* LDAP_SLAPI */
376
377                         defref = op->o_bd->be_update_refs
378                                 ? op->o_bd->be_update_refs : default_referral;
379
380                         if ( defref != NULL ) {
381                                 rs->sr_ref = referral_rewrite( defref,
382                                         NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
383                                 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
384                                 rs->sr_err = LDAP_REFERRAL;
385                                 if (!rs->sr_ref) rs->sr_ref = default_referral;
386                                 send_ldap_result( op, rs );
387
388                                 if ( rs->sr_ref != default_referral ) {
389                                         ber_bvarray_free( rs->sr_ref );
390                                 }
391                         } else {
392                                 send_ldap_error( op, rs,
393                                         LDAP_UNWILLING_TO_PERFORM,
394                                         "shadow context; no update referral" );
395                         }
396 #endif /* SLAPD_MULTIMASTER */
397                 }
398         } else {
399 #ifdef LDAP_SLAPI
400                 if ( op->o_pb ) {
401                         rs->sr_err = call_add_preop_plugins( op );
402                         if ( rs->sr_err != LDAP_SUCCESS ) {
403                                 /* plugin will have sent result */
404                                 goto done;
405                         }
406                 }
407 #endif
408             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
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 rc;
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                         if ( dup ) {
471                                 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
472                                         ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
473                                 }
474                                 BER_BVZERO( &attr->a_vals[i+j] );       
475                         } else {
476                                 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
477                                         sizeof( struct berval ) * j );
478                                 ch_free( mods->sml_values );
479                                 mods->sml_values = NULL;
480                         }
481
482                         if( mods->sml_nvalues ) {
483                                 attr->a_nvals = ch_realloc( attr->a_nvals,
484                                         sizeof( struct berval ) * (i+j) );
485                                 if ( dup ) {
486                                         for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
487                                                 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
488                                         }
489                                         BER_BVZERO( &attr->a_nvals[i+j] );      
490                                 } else {
491                                         AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
492                                                 sizeof( struct berval ) * j );
493                                         ch_free( mods->sml_nvalues );
494                                         mods->sml_nvalues = NULL;
495                                 }
496                         } else {
497                                 attr->a_nvals = attr->a_vals;
498                         }
499
500                         continue;
501 #else
502                         snprintf( textbuf, textlen,
503                                 "attribute '%s' provided more than once",
504                                 mods->sml_desc->ad_cname.bv_val );
505                         return LDAP_TYPE_OR_VALUE_EXISTS;
506 #endif
507                 }
508
509                 if( mods->sml_values[1].bv_val != NULL ) {
510                         /* check for duplicates */
511                         int             i, j;
512                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
513
514                         /* check if the values we're adding already exist */
515                         if( mr == NULL || !mr->smr_match ) {
516                                 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
517                                         /* test asserted values against themselves */
518                                         for( j = 0; j < i; j++ ) {
519                                                 if ( bvmatch( &mods->sml_values[i],
520                                                         &mods->sml_values[j] ) )
521                                                 {
522                                                         /* value exists already */
523                                                         snprintf( textbuf, textlen,
524                                                                 "%s: value #%d provided more than once",
525                                                                 mods->sml_desc->ad_cname.bv_val, j );
526                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
527                                                 }
528                                         }
529                                 }
530
531                         } else {
532                                 int     rc;
533                                 int match;
534
535                                 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
536                                         /* test asserted values against themselves */
537                                         for( j = 0; j < i; j++ ) {
538                                                 rc = value_match( &match, mods->sml_desc, mr,
539                                                         SLAP_MR_EQUALITY
540                                                         | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
541                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
542                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
543                                                         mods->sml_nvalues
544                                                                 ? &mods->sml_nvalues[i]
545                                                                 : &mods->sml_values[i],
546                                                         mods->sml_nvalues
547                                                                 ? &mods->sml_nvalues[j]
548                                                                 : &mods->sml_values[j],
549                                                         text );
550
551                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
552                                                         /* value exists already */
553                                                         snprintf( textbuf, textlen,
554                                                                 "%s: value #%d provided more than once",
555                                                                 mods->sml_desc->ad_cname.bv_val, j );
556                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
557
558                                                 } else if ( rc != LDAP_SUCCESS ) {
559                                                         return rc;
560                                                 }
561                                         }
562                                 }
563                         }
564                 }
565
566                 attr = ch_calloc( 1, sizeof(Attribute) );
567
568                 /* move ad to attr structure */
569                 attr->a_desc = mods->sml_desc;
570                 if ( !dup ) mods->sml_desc = NULL;
571
572                 /* move values to attr structure */
573                 /*      should check for duplicates */
574                 if ( dup ) { 
575                         int i;
576                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
577                         attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
578                         for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
579                                 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
580                         }
581                         BER_BVZERO( &attr->a_vals[i] );
582                 } else {
583                         attr->a_vals = mods->sml_values;
584                         mods->sml_values = NULL;
585                 }
586
587                 if ( mods->sml_nvalues ) {
588                         if ( dup ) {
589                                 int i;
590                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
591                                 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
592                                 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
593                                         ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
594                                 }
595                                 BER_BVZERO( &attr->a_nvals[i] );
596                         } else {
597                                 attr->a_nvals = mods->sml_nvalues;
598                                 mods->sml_nvalues = NULL;
599                         }
600                 } else {
601                         attr->a_nvals = attr->a_vals;
602                 }
603
604                 *tail = attr;
605                 tail = &attr->a_next;
606         }
607
608         *text = NULL;
609
610         return LDAP_SUCCESS;
611 }
612
613 int
614 slap_entry2mods(
615         Entry *e,
616         Modifications **mods,
617         const char **text,
618         char *textbuf, size_t textlen )
619 {
620         Modifications   *modhead = NULL;
621         Modifications   *mod;
622         Modifications   **modtail = &modhead;
623         Attribute               *a_new;
624         AttributeDescription    *a_new_desc;
625         int                             i, count;
626
627         a_new = e->e_attrs;
628
629         while ( a_new != NULL ) {
630                 a_new_desc = a_new->a_desc;
631                 mod = (Modifications *) malloc( sizeof( Modifications ));
632                 
633                 mod->sml_op = LDAP_MOD_REPLACE;
634
635                 mod->sml_type = a_new_desc->ad_cname;
636
637                 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
638
639                 mod->sml_values = (struct berval*) malloc(
640                         (count+1) * sizeof( struct berval) );
641
642                 /* see slap_mods_check() comments...
643                  * if a_vals == a_nvals, there is no normalizer.
644                  * in this case, mod->sml_nvalues must be left NULL.
645                  */
646                 if ( a_new->a_vals != a_new->a_nvals ) {
647                         mod->sml_nvalues = (struct berval*) malloc(
648                                 (count+1) * sizeof( struct berval) );
649                 } else {
650                         mod->sml_nvalues = NULL;
651                 }
652
653                 for ( i = 0; i < count; i++ ) {
654                         ber_dupbv(mod->sml_values+i, a_new->a_vals+i); 
655                         if ( mod->sml_nvalues ) {
656                                 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i ); 
657                         } 
658                 }
659
660                 mod->sml_values[count].bv_val = NULL; 
661                 mod->sml_values[count].bv_len = 0; 
662
663                 if ( mod->sml_nvalues ) {
664                         mod->sml_nvalues[count].bv_val = NULL; 
665                         mod->sml_nvalues[count].bv_len = 0; 
666                 }
667
668                 mod->sml_desc = a_new_desc;
669                 mod->sml_next =NULL;
670                 *modtail = mod;
671                 modtail = &mod->sml_next;
672                 a_new = a_new->a_next; 
673         }
674
675         *mods = modhead;
676
677         return LDAP_SUCCESS;
678 }
679
680 #ifdef LDAP_SLAPI
681 static void init_add_pblock( Operation *op,
682         struct berval *dn, Entry *e, int manageDSAit )
683 {
684         slapi_int_pblock_set_operation( op->o_pb, op );
685         slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
686         slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
687         slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
688 }
689
690 static int call_add_preop_plugins( Operation *op )
691 {
692         int rc;
693
694         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
695         if ( rc < 0 ) {
696                 /*
697                  * A preoperation plugin failure will abort the
698                  * entire operation.
699                  */
700                 Debug(LDAP_DEBUG_TRACE,
701                         "do_add: add preoperation plugin failed.\n",
702                         0, 0, 0);
703
704                 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
705                         (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
706                 {
707                         rc = LDAP_OTHER;
708                 }
709         } else {
710                 rc = LDAP_SUCCESS;
711         }
712
713         return rc;
714 }
715
716 static void call_add_postop_plugins( Operation *op )
717 {
718         int rc;
719
720         rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
721         if ( rc < 0 ) {
722                 Debug(LDAP_DEBUG_TRACE,
723                         "do_add: add postoperation plugin failed\n",
724                         0, 0, 0);
725         }
726 }
727 #endif /* LDAP_SLAPI */