3 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1995 Regents of the University of Michigan.
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.
21 #include <ac/string.h>
23 #include <ac/socket.h>
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 */
37 do_add( Connection *conn, Operation *op )
39 BerElement *ber = op->o_ber;
41 struct berval dn = { 0, NULL };
46 Modifications *modlist = NULL;
47 Modifications **modtail = &modlist;
48 Modifications tmp, *mod;
52 int rc = LDAP_SUCCESS;
55 Slapi_PBlock *pb = NULL;
56 #endif /* LDAP_SLAPI */
59 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
61 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
64 * Parse the add request. It looks like this:
66 * AddRequest := [APPLICATION 14] SEQUENCE {
67 * name DistinguishedName,
68 * attrs SEQUENCE OF SEQUENCE {
70 * values SET OF AttributeValue
76 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
78 LDAP_LOG( OPERATION, ERR,
79 "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
81 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
83 send_ldap_disconnect( conn, op,
84 LDAP_PROTOCOL_ERROR, "decoding error" );
88 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
90 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
92 if( rc != LDAP_SUCCESS ) {
94 LDAP_LOG( OPERATION, ERR,
95 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
97 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
99 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
100 "invalid DN", NULL, NULL );
105 LDAP_LOG( OPERATION, ARGS,
106 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
108 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
112 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
113 tag = ber_next_element( ber, &len, last ) )
118 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
120 if ( rtag == LBER_ERROR ) {
122 LDAP_LOG( OPERATION, ERR,
123 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
125 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
127 send_ldap_disconnect( conn, op,
128 LDAP_PROTOCOL_ERROR, "decoding error" );
133 if ( tmp.sml_bvalues == NULL ) {
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 );
139 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
140 tmp.sml_type.bv_val, 0, 0 );
142 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
143 NULL, "no values for attribute type", NULL, NULL );
146 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
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;
155 modtail = &mod->sml_next;
158 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
160 LDAP_LOG( OPERATION, ERR,
161 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
163 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
165 send_ldap_disconnect( conn, op,
166 LDAP_PROTOCOL_ERROR, "decoding error" );
171 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
173 LDAP_LOG( OPERATION, INFO,
174 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
176 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
181 if ( modlist == NULL ) {
182 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
183 NULL, "no attributes provided", NULL, NULL );
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 );
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",
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",
205 * is objectClass special?
208 /* look for objectClass attribute */
209 for ( mod = modlist; mod; mod = mod->sml_next ) {
210 AttributeDescription *mod_desc = NULL;
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 );
219 if ( mod_desc == slap_schema.si_ad_objectClass ) {
225 send_ldap_result( conn, op,
226 rc = LDAP_OBJECT_CLASS_VIOLATION,
227 NULL, "objectClass missing",
232 /* look for special objectClass */
233 for ( cnt = 0; mod->sml_bvalues[ cnt ].bv_val; cnt++ ) {
236 oc = oc_bvfind( &mod->sml_bvalues[ cnt ] );
239 send_ldap_result( conn, op,
240 rc = LDAP_OBJECT_CLASS_VIOLATION,
241 NULL, "undefined objectClass",
247 * check for special objectClasses: alias, to allow
249 * dn: DC=alias,DC=example,DC=net
250 * aliasedObjectName: DC=aliased,DC=example,DC=net
253 if ( oc == slap_schema.si_oc_alias ) {
258 * FIXME: a referral should be implemented
259 * as exemplified in RFC3296:
261 * dn: DC=sub,DC=example,DC=net
263 * ref: ldap://B/DC=sub,DC=example,DC=net
264 * objectClass: referral
265 * objectClass: extensibleObject
267 * in this case there would be no need to treat
268 * the "referral" objectClass as special.
270 if ( oc == slap_schema.si_oc_referral ) {
278 if ( mod->sml_bvalues[ cnt ].bv_val == NULL ) {
281 * Get attribute type(s) and attribute value(s) of our rdn,
283 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
284 LDAP_DN_FORMAT_LDAP ) )
286 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
287 NULL, "unknown type(s) used in RDN",
292 /* Check for RDN attrs in entry */
293 for ( cnt = 0; rdn[ 0 ][ cnt ]; cnt++ ) {
294 AttributeDescription *desc = NULL;
298 rc = slap_bv2ad( &rdn[ 0 ][ cnt ]->la_attr,
301 if ( rc != LDAP_SUCCESS ) {
302 send_ldap_result( conn, op, rc,
303 NULL, text, NULL, NULL );
307 for (mod = modlist; mod; mod = mod->sml_next) {
308 AttributeDescription *mod_desc = NULL;
310 rc = slap_bv2ad( &mod->sml_type,
312 if ( rc != LDAP_SUCCESS ) {
313 send_ldap_result( conn, op, rc,
314 NULL, text, NULL, NULL );
318 if (mod_desc == desc) {
327 send_ldap_result( conn, op,
328 rc = LDAP_NO_SUCH_ATTRIBUTE,
330 "attribute in RDN not listed in entry",
334 #else /* ! BAILOUT */
337 /* add attribute type and value to modlist */
338 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
340 mod->sml_op = LDAP_MOD_ADD;
341 mod->sml_next = NULL;
342 mod->sml_desc = NULL;
344 ber_dupbv( &mod->sml_type,
345 &rdn[ 0 ][ cnt ]->la_attr );
347 mod->sml_bvalues = NULL;
348 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
349 ber_bvarray_add( &mod->sml_bvalues, &bv );
352 modtail = &mod->sml_next;
354 #endif /* ! BAILOUT */
357 mr = desc->ad_type->sat_equality;
358 if (mr == NULL || !mr->smr_match ) {
360 send_ldap_result( conn, op,
361 rc = LDAP_INVALID_SYNTAX,
363 "attribute in RDN lacks matching rule",
368 for (i = 0; mod->sml_bvalues[ i ].bv_val; i++) {
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);
376 if ( rc != LDAP_SUCCESS ) {
377 send_ldap_result( conn, op, rc,
378 NULL, text, NULL, NULL);
388 if (mod->sml_bvalues[ i ].bv_val == NULL) {
391 send_ldap_result( conn, op,
392 rc = LDAP_NO_SUCH_ATTRIBUTE,
394 "value in RDN not listed in entry",
398 #else /* ! BAILOUT */
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 );
405 #endif /* ! BAILOUT */
410 manageDSAit = get_manageDSAit( op );
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.
417 be = select_backend( &e->e_nname, manageDSAit, 0 );
419 BerVarray ref = referral_rewrite( default_referral,
420 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
422 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
423 NULL, NULL, ref ? ref : default_referral, NULL );
425 if ( ref ) ber_bvarray_free( ref );
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 );
437 /* check for referrals */
438 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
439 if ( rc != LDAP_SUCCESS ) {
444 pb = initAddPlugin( be, conn, op, &dn, e, manageDSAit );
445 #endif /* LDAP_SLAPI */
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.
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 )
460 int update = be->be_update_ndn.bv_len;
461 char textbuf[SLAP_TEXT_BUFLEN];
462 size_t textlen = sizeof textbuf;
464 rc = slap_mods_check( modlist, update, &text,
467 if( rc != LDAP_SUCCESS ) {
468 send_ldap_result( conn, op, rc,
469 NULL, text, NULL, NULL );
474 for( modtail = &modlist;
476 modtail = &(*modtail)->sml_next )
478 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
479 assert( (*modtail)->sml_desc != NULL );
481 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
483 if( rc != LDAP_SUCCESS ) {
484 send_ldap_result( conn, op, rc,
485 NULL, text, NULL, NULL );
490 rc = slap_mods2entry( modlist, &e, repl_user, &text,
492 if( rc != LDAP_SUCCESS ) {
493 send_ldap_result( conn, op, rc,
494 NULL, text, NULL, NULL );
500 * Call the preoperation plugin here, because the entry
501 * will actually contain something.
503 rc = doPreAddPluginFNs( be, pb );
504 if ( rc != LDAP_SUCCESS ) {
505 /* plugin will have sent result */
508 #endif /* LDAP_SLAPI */
510 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
511 #ifdef SLAPD_MULTIMASTER
515 replog( be, op, &e->e_name, &e->e_nname, e );
517 be_entry_release_w( be, conn, op, e );
521 #ifndef SLAPD_MULTIMASTER
527 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
528 * on replicas (for now, it involves the minimum code intrusion).
530 rc = doPreAddPluginFNs( be, pb );
531 if ( rc != LDAP_SUCCESS ) {
532 /* plugin will have sent result */
535 #endif /* LDAP_SLAPI */
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 );
542 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
543 ref ? ref : defref, NULL );
545 if ( ref ) ber_bvarray_free( ref );
546 #endif /* SLAPD_MULTIMASTER */
549 rc = doPreAddPluginFNs( be, pb );
550 if ( rc != LDAP_SUCCESS ) {
551 /* plugin will have sent result */
555 LDAP_LOG( OPERATION, INFO,
556 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
558 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
560 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
561 NULL, "operation not supported within namingContext", NULL, NULL );
565 doPostAddPluginFNs( be, pb );
566 #endif /* LDAP_SLAPI */
569 if( modlist != NULL ) {
570 slap_mods_free( modlist );
585 char *textbuf, size_t textlen )
587 Attribute **tail = &(*e)->e_attrs;
588 assert( *tail == NULL );
592 for( ; mods != NULL; mods = mods->sml_next ) {
595 assert( mods->sml_op == LDAP_MOD_ADD );
596 assert( mods->sml_desc != NULL );
598 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
601 #define SLURPD_FRIENDLY
602 #ifdef SLURPD_FRIENDLY
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;
612 for( i=0; attr->a_vals[i].bv_val; i++ ) {
615 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
620 attr->a_vals = ch_realloc( attr->a_vals,
621 sizeof( struct berval ) * (i+j) );
623 /* should check for duplicates */
625 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
626 sizeof( struct berval ) * j );
628 /* trim the mods array */
629 ch_free( mods->sml_bvalues );
630 mods->sml_bvalues = NULL;
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;
641 if( mods->sml_bvalues[1].bv_val != NULL ) {
642 /* check for duplicates */
644 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
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;
664 const char *text = NULL;
665 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
667 rc = modify_check_duplicates( mods->sml_desc, mr,
668 NULL, mods->sml_bvalues, 0,
669 &text, textbuf, sizeof( textbuf ) );
671 if ( rc != LDAP_SUCCESS ) {
677 attr = ch_calloc( 1, sizeof(Attribute) );
679 /* move ad to attr structure */
680 attr->a_desc = mods->sml_desc;
681 mods->sml_desc = NULL;
683 /* move values to attr structure */
684 /* should check for duplicates */
685 attr->a_vals = mods->sml_bvalues;
686 mods->sml_bvalues = NULL;
689 tail = &attr->a_next;
696 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
697 struct berval *dn, Entry *e, int manageDSAit )
703 slapi_x_backend_set_pb( pb, be );
704 slapi_x_connection_set_pb( pb, conn );
705 slapi_x_operation_set_pb( pb, op );
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 );
714 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb )
718 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
721 * A preoperation plugin failure will abort the
725 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
728 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
730 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
740 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb )
744 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
747 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
750 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
755 #endif /* LDAP_SLAPI */