]> git.sur5r.net Git - openldap/blob - servers/slapd/add.c
do not treat extensibleObject as special; add comment about referral
[openldap] / servers / slapd / add.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21 #include <ac/string.h>
22 #include <ac/time.h>
23 #include <ac/socket.h>
24
25 #include "ldap_pvt.h"
26 #include "slap.h"
27 #include "slapi.h"
28
29 #ifdef LDAP_SLAPI
30 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
31         struct berval *dn, Entry *e, int manageDSAit );
32 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb );
33 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb );
34 #endif /* LDAP_SLAPI */
35
36 int
37 do_add( Connection *conn, Operation *op )
38 {
39         BerElement      *ber = op->o_ber;
40         char            *last;
41         struct berval dn = { 0, NULL };
42         ber_len_t       len;
43         ber_tag_t       tag;
44         Entry           *e;
45         Backend         *be;
46         Modifications   *modlist = NULL;
47         Modifications   **modtail = &modlist;
48         Modifications   tmp, *mod;
49         const char *text;
50         LDAPRDN         *rdn = NULL;
51         int             cnt;
52         int                     rc = LDAP_SUCCESS;
53         int     manageDSAit;
54 #ifdef LDAP_SLAPI
55         Slapi_PBlock    *pb = NULL;
56 #endif /* LDAP_SLAPI */
57
58 #ifdef NEW_LOGGING
59         LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_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", conn->c_connid,0,0 );
80 #else
81                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
82 #endif
83                 send_ldap_disconnect( conn, op,
84                         LDAP_PROTOCOL_ERROR, "decoding error" );
85                 return -1;
86         }
87
88         e = (Entry *) ch_calloc( 1, sizeof(Entry) );
89
90         rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
91
92         if( rc != LDAP_SUCCESS ) {
93 #ifdef NEW_LOGGING
94                 LDAP_LOG( OPERATION, ERR, 
95                         "do_add: conn %d invalid dn (%s)\n", conn->c_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_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
100                             "invalid DN", NULL, NULL );
101                 goto done;
102         }
103
104 #ifdef NEW_LOGGING
105         LDAP_LOG( OPERATION, ARGS, 
106                 "do_add: conn %d  dn (%s)\n", conn->c_connid, e->e_dn, 0 );
107 #else
108         Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
109 #endif
110
111         /* get the attrs */
112         for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
113             tag = ber_next_element( ber, &len, last ) )
114         {
115                 Modifications *mod;
116                 ber_tag_t rtag;
117
118                 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
119
120                 if ( rtag == LBER_ERROR ) {
121 #ifdef NEW_LOGGING
122                         LDAP_LOG( OPERATION, ERR, 
123                                    "do_add: conn %d      decoding error \n", conn->c_connid, 0, 0 );
124 #else
125                         Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
126 #endif
127                         send_ldap_disconnect( conn, op,
128                                 LDAP_PROTOCOL_ERROR, "decoding error" );
129                         rc = -1;
130                         goto done;
131                 }
132
133                 if ( tmp.sml_bvalues == NULL ) {
134 #ifdef NEW_LOGGING
135                         LDAP_LOG( OPERATION, INFO, 
136                                 "do_add: conn %d         no values for type %s\n",
137                                 conn->c_connid, tmp.sml_type.bv_val, 0 );
138 #else
139                         Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
140                                 tmp.sml_type.bv_val, 0, 0 );
141 #endif
142                         send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
143                                 NULL, "no values for attribute type", NULL, NULL );
144                         goto done;
145                 }
146                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
147                 
148                 mod->sml_op = LDAP_MOD_ADD;
149                 mod->sml_next = NULL;
150                 mod->sml_desc = NULL;
151                 mod->sml_type = tmp.sml_type;
152                 mod->sml_bvalues = tmp.sml_bvalues;
153
154                 *modtail = mod;
155                 modtail = &mod->sml_next;
156         }
157
158         if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
159 #ifdef NEW_LOGGING
160                 LDAP_LOG( OPERATION, ERR, 
161                         "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
162 #else
163                 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
164 #endif
165                 send_ldap_disconnect( conn, op,
166                         LDAP_PROTOCOL_ERROR, "decoding error" );
167                 rc = -1;
168                 goto done;
169         }
170
171         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
172 #ifdef NEW_LOGGING
173                 LDAP_LOG( OPERATION, INFO, 
174                         "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
175 #else
176                 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
177 #endif
178                 goto done;
179         } 
180
181         if ( modlist == NULL ) {
182                 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
183                         NULL, "no attributes provided", NULL, NULL );
184                 goto done;
185         }
186
187         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
188             op->o_connid, op->o_opid, e->e_dn, 0, 0 );
189
190         if( e->e_nname.bv_len == 0 ) {
191                 /* protocolError may be a more appropriate error */
192                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
193                         NULL, "root DSE already exists",
194                         NULL, NULL );
195                 goto done;
196
197         } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
198                 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
199                         NULL, "subschema subentry already exists",
200                         NULL, NULL );
201                 goto done;
202         }
203
204         /*
205          * is objectClass special?
206          */
207
208         /* look for objectClass attribute */
209         for ( mod = modlist; mod; mod = mod->sml_next ) {
210                 AttributeDescription    *mod_desc = NULL;
211
212                 rc = slap_bv2ad( &mod->sml_type, &mod_desc, &text );
213                 if ( rc != LDAP_SUCCESS ) {
214                         send_ldap_result( conn, op, rc,
215                                         NULL, text, NULL, NULL );
216                         goto done;
217                 }
218
219                 if ( mod_desc == slap_schema.si_ad_objectClass ) {
220                         break;
221                 }
222         }
223
224         if ( mod == NULL ) {
225                 send_ldap_result( conn, op, 
226                                 rc = LDAP_OBJECT_CLASS_VIOLATION,
227                                 NULL, "objectClass missing", 
228                                 NULL, NULL );
229                 goto done;
230         }
231
232         /* look for special objectClass */
233         for ( cnt = 0; mod->sml_bvalues[ cnt ].bv_val; cnt++ ) {
234                 ObjectClass     *oc;
235
236                 oc = oc_bvfind( &mod->sml_bvalues[ cnt ] );
237
238                 if ( oc == NULL ) {
239                         send_ldap_result( conn, op, 
240                                         rc = LDAP_OBJECT_CLASS_VIOLATION,
241                                         NULL, "undefined objectClass", 
242                                         NULL, NULL );
243                         goto done;
244                 }
245
246                 /*
247                  * check for special objectClasses: alias, to allow
248                  *
249                  * dn: DC=alias,DC=example,DC=net
250                  * aliasedObjectName: DC=aliased,DC=example,DC=net
251                  * objectClass: alias
252                  */
253                 if ( oc == slap_schema.si_oc_alias ) {
254                         break;
255                 }
256
257                 /*
258                  * FIXME: a referral should be implemented
259                  * as exemplified in RFC3296:
260                  *
261                  *       dn: DC=sub,DC=example,DC=net
262                  *       dc: sub
263                  *       ref: ldap://B/DC=sub,DC=example,DC=net 
264                  *       objectClass: referral
265                  *       objectClass: extensibleObject 
266                  *
267                  * in this case there would be no need to treat
268                  * the "referral" objectClass as special.
269                  */
270                 if ( oc == slap_schema.si_oc_referral ) {
271                         break;
272                 }
273         }
274
275         /*
276          * if not special
277          */
278         if ( mod->sml_bvalues[ cnt ].bv_val == NULL ) {
279
280                 /*
281                  * Get attribute type(s) and attribute value(s) of our rdn,
282                  */
283                 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
284                         LDAP_DN_FORMAT_LDAP ) )
285                 {
286                         send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
287                                 NULL, "unknown type(s) used in RDN",
288                                 NULL, NULL );
289                         goto done;
290                 }
291
292                 /* Check for RDN attrs in entry */
293                 for ( cnt = 0; rdn[ 0 ][ cnt ]; cnt++ ) {
294                         AttributeDescription    *desc = NULL;
295                         MatchingRule            *mr;
296                         int                     i;
297         
298                         rc = slap_bv2ad( &rdn[ 0 ][ cnt ]->la_attr, 
299                                         &desc, &text );
300         
301                         if ( rc != LDAP_SUCCESS ) {
302                                 send_ldap_result( conn, op, rc,
303                                                 NULL, text, NULL, NULL );
304                                 goto done;
305                         }
306         
307                         for (mod = modlist; mod; mod = mod->sml_next) {
308                                 AttributeDescription    *mod_desc = NULL;
309         
310                                 rc = slap_bv2ad( &mod->sml_type, 
311                                                 &mod_desc, &text );
312                                 if ( rc != LDAP_SUCCESS ) {
313                                         send_ldap_result( conn, op, rc,
314                                                         NULL, text, NULL, NULL );
315                                         goto done;
316                                 }
317         
318                                 if (mod_desc == desc) {
319                                         break;
320                                 }
321                         }
322         
323                         if (mod == NULL) {
324 #define BAILOUT
325 #ifdef BAILOUT
326                                 /* bail out */
327                                 send_ldap_result( conn, op, 
328                                                 rc = LDAP_NO_SUCH_ATTRIBUTE,
329                                                 NULL,
330                                                 "attribute in RDN not listed in entry", 
331                                                 NULL, NULL );
332                                 goto done;
333         
334 #else /* ! BAILOUT */
335                                 struct berval   bv;
336         
337                                 /* add attribute type and value to modlist */
338                                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
339                         
340                                 mod->sml_op = LDAP_MOD_ADD;
341                                 mod->sml_next = NULL;
342                                 mod->sml_desc = NULL;
343         
344                                 ber_dupbv( &mod->sml_type,
345                                                 &rdn[ 0 ][ cnt ]->la_attr );
346         
347                                 mod->sml_bvalues = NULL;
348                                 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
349                                 ber_bvarray_add( &mod->sml_bvalues, &bv );
350         
351                                 *modtail = mod;
352                                 modtail = &mod->sml_next;
353                                 continue;
354 #endif /* ! BAILOUT */
355                         }
356         
357                         mr = desc->ad_type->sat_equality;
358                         if (mr == NULL || !mr->smr_match ) {
359                                 /* bail out */
360                                 send_ldap_result( conn, op, 
361                                                 rc = LDAP_INVALID_SYNTAX,
362                                                 NULL,
363                                                 "attribute in RDN lacks matching rule", 
364                                                 NULL, NULL );
365                                 goto done;
366                         }
367         
368                         for (i = 0; mod->sml_bvalues[ i ].bv_val; i++) {
369                                 int             match = 0;
370                                 
371                                 rc = value_match(&match, desc, mr,
372                                                 SLAP_MR_VALUE_SYNTAX_MATCH,
373                                                 &mod->sml_bvalues[ i ],
374                                                 &rdn[ 0 ][ cnt ]->la_value, &text);
375         
376                                 if ( rc != LDAP_SUCCESS ) {
377                                         send_ldap_result( conn, op, rc,
378                                                         NULL, text, NULL, NULL);
379                                         goto done;
380                                 }
381         
382                                 if (match == 0) {
383                                         break;
384                                 }
385                         }
386         
387                         /* not found? */
388                         if (mod->sml_bvalues[ i ].bv_val == NULL) {
389 #ifdef BAILOUT
390                                 /* bailout */
391                                 send_ldap_result( conn, op, 
392                                                 rc = LDAP_NO_SUCH_ATTRIBUTE,
393                                                 NULL,
394                                                 "value in RDN not listed in entry", 
395                                                 NULL, NULL );
396                                 goto done;
397         
398 #else /* ! BAILOUT */
399                                 struct berval   bv;
400         
401                                 /* add attribute type and value to modlist */
402                                 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
403                                 ber_bvarray_add( &mod->sml_bvalues, &bv );
404                                 continue;
405 #endif /* ! BAILOUT */
406                         }
407                 }
408         }
409
410         manageDSAit = get_manageDSAit( op );
411
412         /*
413          * We could be serving multiple database backends.  Select the
414          * appropriate one, or send a referral to our "referral server"
415          * if we don't hold it.
416          */
417         be = select_backend( &e->e_nname, manageDSAit, 0 );
418         if ( be == NULL ) {
419                 BerVarray ref = referral_rewrite( default_referral,
420                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
421
422                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
423                         NULL, NULL, ref ? ref : default_referral, NULL );
424
425                 if ( ref ) ber_bvarray_free( ref );
426                 goto done;
427         }
428
429         /* check restrictions */
430         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
431         if( rc != LDAP_SUCCESS ) {
432                 send_ldap_result( conn, op, rc,
433                         NULL, text, NULL, NULL );
434                 goto done;
435         }
436
437         /* check for referrals */
438         rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
439         if ( rc != LDAP_SUCCESS ) {
440                 goto done;
441         }
442
443 #ifdef LDAP_SLAPI
444         pb = initAddPlugin( be, conn, op, &dn, e, manageDSAit );
445 #endif /* LDAP_SLAPI */
446
447         /*
448          * do the add if 1 && (2 || 3)
449          * 1) there is an add function implemented in this backend;
450          * 2) this backend is master for what it holds;
451          * 3) it's a replica and the dn supplied is the updatedn.
452          */
453         if ( be->be_add ) {
454                 /* do the update here */
455                 int repl_user = be_isupdate(be, &op->o_ndn );
456 #ifndef SLAPD_MULTIMASTER
457                 if ( !be->be_update_ndn.bv_len || repl_user )
458 #endif
459                 {
460                         int update = be->be_update_ndn.bv_len;
461                         char textbuf[SLAP_TEXT_BUFLEN];
462                         size_t textlen = sizeof textbuf;
463
464                         rc = slap_mods_check( modlist, update, &text,
465                                 textbuf, textlen );
466
467                         if( rc != LDAP_SUCCESS ) {
468                                 send_ldap_result( conn, op, rc,
469                                         NULL, text, NULL, NULL );
470                                 goto done;
471                         }
472
473                         if ( !repl_user ) {
474                                 for( modtail = &modlist;
475                                         *modtail != NULL;
476                                         modtail = &(*modtail)->sml_next )
477                                 {
478                                         assert( (*modtail)->sml_op == LDAP_MOD_ADD );
479                                         assert( (*modtail)->sml_desc != NULL );
480                                 }
481                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
482                                         textbuf, textlen );
483                                 if( rc != LDAP_SUCCESS ) {
484                                         send_ldap_result( conn, op, rc,
485                                                 NULL, text, NULL, NULL );
486                                         goto done;
487                                 }
488                         }
489
490                         rc = slap_mods2entry( modlist, &e, repl_user, &text,
491                                 textbuf, textlen );
492                         if( rc != LDAP_SUCCESS ) {
493                                 send_ldap_result( conn, op, rc,
494                                         NULL, text, NULL, NULL );
495                                 goto done;
496                         }
497
498 #ifdef LDAP_SLAPI
499                         /*
500                          * Call the preoperation plugin here, because the entry
501                          * will actually contain something.
502                          */
503                         rc = doPreAddPluginFNs( be, pb );
504                         if ( rc != LDAP_SUCCESS ) {
505                                 /* plugin will have sent result */
506                                 goto done;
507                         }
508 #endif /* LDAP_SLAPI */
509
510                         if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
511 #ifdef SLAPD_MULTIMASTER
512                                 if ( !repl_user )
513 #endif
514                                 {
515                                         replog( be, op, &e->e_name, &e->e_nname, e );
516                                 }
517                                 be_entry_release_w( be, conn, op, e );
518                                 e = NULL;
519                         }
520
521 #ifndef SLAPD_MULTIMASTER
522                 } else {
523                         BerVarray defref;
524                         BerVarray ref;
525 #ifdef LDAP_SLAPI
526                         /*
527                          * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
528                          * on replicas (for now, it involves the minimum code intrusion).
529                          */
530                         rc = doPreAddPluginFNs( be, pb );
531                         if ( rc != LDAP_SUCCESS ) {
532                                 /* plugin will have sent result */
533                                 goto done;
534                         }
535 #endif /* LDAP_SLAPI */
536
537                         defref = be->be_update_refs
538                                 ? be->be_update_refs : default_referral;
539                         ref = referral_rewrite( defref,
540                                 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
541
542                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
543                                 ref ? ref : defref, NULL );
544
545                         if ( ref ) ber_bvarray_free( ref );
546 #endif /* SLAPD_MULTIMASTER */
547                 }
548         } else {
549             rc = doPreAddPluginFNs( be, pb );
550             if ( rc != LDAP_SUCCESS ) {
551                 /* plugin will have sent result */
552                 goto done;
553             }
554 #ifdef NEW_LOGGING
555             LDAP_LOG( OPERATION, INFO, 
556                        "do_add: conn %d  no backend support\n", conn->c_connid, 0, 0 );
557 #else
558             Debug( LDAP_DEBUG_ARGS, "    do_add: no backend support\n", 0, 0, 0 );
559 #endif
560             send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
561                               NULL, "operation not supported within namingContext", NULL, NULL );
562         }
563
564 #ifdef LDAP_SLAPI
565         doPostAddPluginFNs( be, pb );
566 #endif /* LDAP_SLAPI */
567
568 done:
569         if( modlist != NULL ) {
570                 slap_mods_free( modlist );
571         }
572         if( e != NULL ) {
573                 entry_free( e );
574         }
575
576         return rc;
577 }
578
579 int
580 slap_mods2entry(
581         Modifications *mods,
582         Entry **e,
583         int repl_user,
584         const char **text,
585         char *textbuf, size_t textlen )
586 {
587         Attribute **tail = &(*e)->e_attrs;
588         assert( *tail == NULL );
589
590         *text = textbuf;
591
592         for( ; mods != NULL; mods = mods->sml_next ) {
593                 Attribute *attr;
594
595                 assert( mods->sml_op == LDAP_MOD_ADD );
596                 assert( mods->sml_desc != NULL );
597
598                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
599
600                 if( attr != NULL ) {
601 #define SLURPD_FRIENDLY
602 #ifdef SLURPD_FRIENDLY
603                         ber_len_t i,j;
604
605                         if( !repl_user ) {
606                                 snprintf( textbuf, textlen,
607                                         "attribute '%s' provided more than once",
608                                         mods->sml_desc->ad_cname.bv_val );
609                                 return LDAP_TYPE_OR_VALUE_EXISTS;
610                         }
611
612                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
613                                 /* count them */
614                         }
615                         for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
616                                 /* count them */
617                         }
618                         j++;    /* NULL */
619                         
620                         attr->a_vals = ch_realloc( attr->a_vals,
621                                 sizeof( struct berval ) * (i+j) );
622
623                         /* should check for duplicates */
624
625                         AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
626                                 sizeof( struct berval ) * j );
627
628                         /* trim the mods array */
629                         ch_free( mods->sml_bvalues );
630                         mods->sml_bvalues = NULL;
631
632                         continue;
633 #else
634                         snprintf( textbuf, textlen,
635                                 "attribute '%s' provided more than once",
636                                 mods->sml_desc->ad_cname.bv_val );
637                         return LDAP_TYPE_OR_VALUE_EXISTS;
638 #endif
639                 }
640
641                 if( mods->sml_bvalues[1].bv_val != NULL ) {
642                         /* check for duplicates */
643                         int             i, j;
644                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
645
646                         /* check if the values we're adding already exist */
647                         if( mr == NULL || !mr->smr_match ) {
648                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
649                                         /* test asserted values against themselves */
650                                         for( j = 0; j < i; j++ ) {
651                                                 if ( bvmatch( &mods->sml_bvalues[i],
652                                                         &mods->sml_bvalues[j] ) ) {
653                                                         /* value exists already */
654                                                         snprintf( textbuf, textlen,
655                                                                 "%s: value #%d provided more than once",
656                                                                 mods->sml_desc->ad_cname.bv_val, j );
657                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
658                                                 }
659                                         }
660                                 }
661
662                         } else {
663                                 int             rc;
664                                 const char      *text = NULL;
665                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
666                                 
667                                 rc = modify_check_duplicates( mods->sml_desc, mr,
668                                                 NULL, mods->sml_bvalues, 0,
669                                                 &text, textbuf, sizeof( textbuf ) );
670
671                                 if ( rc != LDAP_SUCCESS ) {
672                                         return rc;
673                                 }
674                         }
675                 }
676
677                 attr = ch_calloc( 1, sizeof(Attribute) );
678
679                 /* move ad to attr structure */
680                 attr->a_desc = mods->sml_desc;
681                 mods->sml_desc = NULL;
682
683                 /* move values to attr structure */
684                 /*      should check for duplicates */
685                 attr->a_vals = mods->sml_bvalues;
686                 mods->sml_bvalues = NULL;
687
688                 *tail = attr;
689                 tail = &attr->a_next;
690         }
691
692         return LDAP_SUCCESS;
693 }
694
695 #ifdef LDAP_SLAPI
696 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
697         struct berval *dn, Entry *e, int manageDSAit )
698 {
699         Slapi_PBlock *pb;
700
701         pb = op->o_pb;
702
703         slapi_x_backend_set_pb( pb, be );
704         slapi_x_connection_set_pb( pb, conn );
705         slapi_x_operation_set_pb( pb, op );
706
707         slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
708         slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
709         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
710
711         return pb;
712 }
713
714 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb )
715 {
716         int rc;
717
718         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
719         if ( rc != 0 ) {
720                 /*
721                  * A preoperation plugin failure will abort the
722                  * entire operation.
723                  */
724 #ifdef NEW_LOGGING
725                 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
726                                 0, 0, 0);
727 #else
728                 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
729                                 0, 0, 0);
730                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
731                         rc = LDAP_OTHER;
732 #endif
733         } else {
734                 rc = LDAP_SUCCESS;
735         }
736
737         return rc;
738 }
739
740 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb )
741 {
742         int rc;
743
744         rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
745         if ( rc != 0 ) {
746 #ifdef NEW_LOGGING
747                 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
748                                 0, 0, 0);
749 #else
750                 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
751                                 0, 0, 0);
752 #endif
753         }
754 }
755 #endif /* LDAP_SLAPI */