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;
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 * Get attribute type(s) and attribute value(s) of our rdn,
207 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
208 LDAP_DN_FORMAT_LDAP ) )
210 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
211 NULL, "unknown type(s) used in RDN",
216 /* Check for RDN attrs in entry */
217 for ( cnt = 0; rdn[ 0 ][ cnt ]; cnt++ ) {
218 AttributeDescription *desc = NULL;
223 rc = slap_bv2ad( &rdn[ 0 ][ cnt ]->la_attr,
226 if ( rc != LDAP_SUCCESS ) {
227 send_ldap_result( conn, op, rc,
228 NULL, text, NULL, NULL );
232 for (mod = modlist; mod; mod = mod->sml_next) {
233 AttributeDescription *mod_desc = NULL;
235 rc = slap_bv2ad( &mod->sml_type,
237 if ( rc != LDAP_SUCCESS ) {
238 send_ldap_result( conn, op, rc,
239 NULL, text, NULL, NULL );
243 if (mod_desc == desc) {
252 send_ldap_result( conn, op,
253 rc = LDAP_NO_SUCH_ATTRIBUTE,
255 "RDN attribute value assertion not present in entry",
259 #else /* ! BAILOUT */
262 /* add attribute type and value to modlist */
263 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
265 mod->sml_op = LDAP_MOD_ADD;
266 mod->sml_next = NULL;
267 mod->sml_desc = NULL;
269 ber_dupbv( &mod->sml_type,
270 &rdn[ 0 ][ cnt ]->la_attr );
272 mod->sml_bvalues = NULL;
273 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
274 ber_bvarray_add( &mod->sml_bvalues, &bv );
277 modtail = &mod->sml_next;
279 #endif /* ! BAILOUT */
282 mr = desc->ad_type->sat_equality;
283 if (mr == NULL || !mr->smr_match ) {
285 send_ldap_result( conn, op,
286 rc = LDAP_INVALID_SYNTAX,
288 "attribute in RDN lacks matching rule",
293 for (i = 0; mod->sml_bvalues[ i ].bv_val; i++) {
296 rc = value_match(&match, desc, mr,
297 SLAP_MR_VALUE_SYNTAX_MATCH,
298 &mod->sml_bvalues[ i ],
299 &rdn[ 0 ][ cnt ]->la_value, &text);
301 if ( rc != LDAP_SUCCESS ) {
302 send_ldap_result( conn, op, rc,
303 NULL, text, NULL, NULL);
313 if (mod->sml_bvalues[ i ].bv_val == NULL) {
316 send_ldap_result( conn, op,
317 rc = LDAP_NO_SUCH_ATTRIBUTE,
319 "value in RDN not listed in entry",
323 #else /* ! BAILOUT */
326 /* add attribute type and value to modlist */
327 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
328 ber_bvarray_add( &mod->sml_bvalues, &bv );
330 #endif /* ! BAILOUT */
334 manageDSAit = get_manageDSAit( op );
337 * We could be serving multiple database backends. Select the
338 * appropriate one, or send a referral to our "referral server"
339 * if we don't hold it.
341 be = select_backend( &e->e_nname, manageDSAit, 0 );
343 BerVarray ref = referral_rewrite( default_referral,
344 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
346 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
347 NULL, NULL, ref ? ref : default_referral, NULL );
349 if ( ref ) ber_bvarray_free( ref );
353 /* check restrictions */
354 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
355 if( rc != LDAP_SUCCESS ) {
356 send_ldap_result( conn, op, rc,
357 NULL, text, NULL, NULL );
361 /* check for referrals */
362 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
363 if ( rc != LDAP_SUCCESS ) {
368 pb = initAddPlugin( be, conn, op, &dn, e, manageDSAit );
369 #endif /* LDAP_SLAPI */
372 * do the add if 1 && (2 || 3)
373 * 1) there is an add function implemented in this backend;
374 * 2) this backend is master for what it holds;
375 * 3) it's a replica and the dn supplied is the updatedn.
378 /* do the update here */
379 int repl_user = be_isupdate(be, &op->o_ndn );
380 #ifndef SLAPD_MULTIMASTER
381 if ( !be->be_update_ndn.bv_len || repl_user )
384 int update = be->be_update_ndn.bv_len;
385 char textbuf[SLAP_TEXT_BUFLEN];
386 size_t textlen = sizeof textbuf;
388 rc = slap_mods_check( modlist, update, &text,
391 if( rc != LDAP_SUCCESS ) {
392 send_ldap_result( conn, op, rc,
393 NULL, text, NULL, NULL );
398 for( modtail = &modlist;
400 modtail = &(*modtail)->sml_next )
402 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
403 assert( (*modtail)->sml_desc != NULL );
405 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
407 if( rc != LDAP_SUCCESS ) {
408 send_ldap_result( conn, op, rc,
409 NULL, text, NULL, NULL );
414 rc = slap_mods2entry( modlist, &e, repl_user, &text,
416 if( rc != LDAP_SUCCESS ) {
417 send_ldap_result( conn, op, rc,
418 NULL, text, NULL, NULL );
424 * Call the preoperation plugin here, because the entry
425 * will actually contain something.
427 rc = doPreAddPluginFNs( be, pb );
428 if ( rc != LDAP_SUCCESS ) {
429 /* plugin will have sent result */
432 #endif /* LDAP_SLAPI */
434 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
435 #ifdef SLAPD_MULTIMASTER
439 replog( be, op, &e->e_name, &e->e_nname, e );
441 be_entry_release_w( be, conn, op, e );
445 #ifndef SLAPD_MULTIMASTER
451 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
452 * on replicas (for now, it involves the minimum code intrusion).
454 rc = doPreAddPluginFNs( be, pb );
455 if ( rc != LDAP_SUCCESS ) {
456 /* plugin will have sent result */
459 #endif /* LDAP_SLAPI */
461 defref = be->be_update_refs
462 ? be->be_update_refs : default_referral;
463 ref = referral_rewrite( defref,
464 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
466 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
467 ref ? ref : defref, NULL );
469 if ( ref ) ber_bvarray_free( ref );
470 #endif /* SLAPD_MULTIMASTER */
473 rc = doPreAddPluginFNs( be, pb );
474 if ( rc != LDAP_SUCCESS ) {
475 /* plugin will have sent result */
479 LDAP_LOG( OPERATION, INFO,
480 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
482 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
484 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
485 NULL, "operation not supported within namingContext", NULL, NULL );
489 doPostAddPluginFNs( be, pb );
490 #endif /* LDAP_SLAPI */
493 if( modlist != NULL ) {
494 slap_mods_free( modlist );
509 char *textbuf, size_t textlen )
511 Attribute **tail = &(*e)->e_attrs;
512 assert( *tail == NULL );
516 for( ; mods != NULL; mods = mods->sml_next ) {
519 assert( mods->sml_op == LDAP_MOD_ADD );
520 assert( mods->sml_desc != NULL );
522 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
525 #define SLURPD_FRIENDLY
526 #ifdef SLURPD_FRIENDLY
530 snprintf( textbuf, textlen,
531 "attribute '%s' provided more than once",
532 mods->sml_desc->ad_cname.bv_val );
533 return LDAP_TYPE_OR_VALUE_EXISTS;
536 for( i=0; attr->a_vals[i].bv_val; i++ ) {
539 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
544 attr->a_vals = ch_realloc( attr->a_vals,
545 sizeof( struct berval ) * (i+j) );
547 /* should check for duplicates */
549 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
550 sizeof( struct berval ) * j );
552 /* trim the mods array */
553 ch_free( mods->sml_bvalues );
554 mods->sml_bvalues = NULL;
558 snprintf( textbuf, textlen,
559 "attribute '%s' provided more than once",
560 mods->sml_desc->ad_cname.bv_val );
561 return LDAP_TYPE_OR_VALUE_EXISTS;
565 if( mods->sml_bvalues[1].bv_val != NULL ) {
566 /* check for duplicates */
568 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
570 /* check if the values we're adding already exist */
571 if( mr == NULL || !mr->smr_match ) {
572 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
573 /* test asserted values against themselves */
574 for( j = 0; j < i; j++ ) {
575 if ( bvmatch( &mods->sml_bvalues[i],
576 &mods->sml_bvalues[j] ) ) {
577 /* value exists already */
578 snprintf( textbuf, textlen,
579 "%s: value #%d provided more than once",
580 mods->sml_desc->ad_cname.bv_val, j );
581 return LDAP_TYPE_OR_VALUE_EXISTS;
588 const char *text = NULL;
589 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
591 rc = modify_check_duplicates( mods->sml_desc, mr,
592 NULL, mods->sml_bvalues, 0,
593 &text, textbuf, sizeof( textbuf ) );
595 if ( rc != LDAP_SUCCESS ) {
601 attr = ch_calloc( 1, sizeof(Attribute) );
603 /* move ad to attr structure */
604 attr->a_desc = mods->sml_desc;
605 mods->sml_desc = NULL;
607 /* move values to attr structure */
608 /* should check for duplicates */
609 attr->a_vals = mods->sml_bvalues;
610 mods->sml_bvalues = NULL;
613 tail = &attr->a_next;
620 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
621 struct berval *dn, Entry *e, int manageDSAit )
627 slapi_x_backend_set_pb( pb, be );
628 slapi_x_connection_set_pb( pb, conn );
629 slapi_x_operation_set_pb( pb, op );
631 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
632 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
633 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
638 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb )
642 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
645 * A preoperation plugin failure will abort the
649 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
652 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
654 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
664 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb )
668 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
671 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
674 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
679 #endif /* LDAP_SLAPI */