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",
204 if ( mod->sml_bvalues[ cnt ].bv_val == NULL ) {
206 * Get attribute type(s) and attribute value(s) of our rdn,
208 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
209 LDAP_DN_FORMAT_LDAP ) )
211 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
212 NULL, "unknown type(s) used in RDN",
217 /* Check for RDN attrs in entry */
218 for ( cnt = 0; rdn[ 0 ][ cnt ]; cnt++ ) {
219 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 */
335 manageDSAit = get_manageDSAit( op );
338 * We could be serving multiple database backends. Select the
339 * appropriate one, or send a referral to our "referral server"
340 * if we don't hold it.
342 be = select_backend( &e->e_nname, manageDSAit, 0 );
344 BerVarray ref = referral_rewrite( default_referral,
345 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
347 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
348 NULL, NULL, ref ? ref : default_referral, NULL );
350 if ( ref ) ber_bvarray_free( ref );
354 /* check restrictions */
355 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
356 if( rc != LDAP_SUCCESS ) {
357 send_ldap_result( conn, op, rc,
358 NULL, text, NULL, NULL );
362 /* check for referrals */
363 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
364 if ( rc != LDAP_SUCCESS ) {
369 pb = initAddPlugin( be, conn, op, &dn, e, manageDSAit );
370 #endif /* LDAP_SLAPI */
373 * do the add if 1 && (2 || 3)
374 * 1) there is an add function implemented in this backend;
375 * 2) this backend is master for what it holds;
376 * 3) it's a replica and the dn supplied is the updatedn.
379 /* do the update here */
380 int repl_user = be_isupdate(be, &op->o_ndn );
381 #ifndef SLAPD_MULTIMASTER
382 if ( !be->be_update_ndn.bv_len || repl_user )
385 int update = be->be_update_ndn.bv_len;
386 char textbuf[SLAP_TEXT_BUFLEN];
387 size_t textlen = sizeof textbuf;
389 rc = slap_mods_check( modlist, update, &text,
392 if( rc != LDAP_SUCCESS ) {
393 send_ldap_result( conn, op, rc,
394 NULL, text, NULL, NULL );
399 for( modtail = &modlist;
401 modtail = &(*modtail)->sml_next )
403 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
404 assert( (*modtail)->sml_desc != NULL );
406 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
408 if( rc != LDAP_SUCCESS ) {
409 send_ldap_result( conn, op, rc,
410 NULL, text, NULL, NULL );
415 rc = slap_mods2entry( modlist, &e, repl_user, &text,
417 if( rc != LDAP_SUCCESS ) {
418 send_ldap_result( conn, op, rc,
419 NULL, text, NULL, NULL );
425 * Call the preoperation plugin here, because the entry
426 * will actually contain something.
428 rc = doPreAddPluginFNs( be, pb );
429 if ( rc != LDAP_SUCCESS ) {
430 /* plugin will have sent result */
433 #endif /* LDAP_SLAPI */
435 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
436 #ifdef SLAPD_MULTIMASTER
440 replog( be, op, &e->e_name, &e->e_nname, e );
442 be_entry_release_w( be, conn, op, e );
446 #ifndef SLAPD_MULTIMASTER
452 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
453 * on replicas (for now, it involves the minimum code intrusion).
455 rc = doPreAddPluginFNs( be, pb );
456 if ( rc != LDAP_SUCCESS ) {
457 /* plugin will have sent result */
460 #endif /* LDAP_SLAPI */
462 defref = be->be_update_refs
463 ? be->be_update_refs : default_referral;
464 ref = referral_rewrite( defref,
465 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
467 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
468 ref ? ref : defref, NULL );
470 if ( ref ) ber_bvarray_free( ref );
471 #endif /* SLAPD_MULTIMASTER */
474 rc = doPreAddPluginFNs( be, pb );
475 if ( rc != LDAP_SUCCESS ) {
476 /* plugin will have sent result */
480 LDAP_LOG( OPERATION, INFO,
481 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
483 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
485 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
486 NULL, "operation not supported within namingContext", NULL, NULL );
490 doPostAddPluginFNs( be, pb );
491 #endif /* LDAP_SLAPI */
494 if( modlist != NULL ) {
495 slap_mods_free( modlist );
510 char *textbuf, size_t textlen )
512 Attribute **tail = &(*e)->e_attrs;
513 assert( *tail == NULL );
517 for( ; mods != NULL; mods = mods->sml_next ) {
520 assert( mods->sml_op == LDAP_MOD_ADD );
521 assert( mods->sml_desc != NULL );
523 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
526 #define SLURPD_FRIENDLY
527 #ifdef SLURPD_FRIENDLY
531 snprintf( textbuf, textlen,
532 "attribute '%s' provided more than once",
533 mods->sml_desc->ad_cname.bv_val );
534 return LDAP_TYPE_OR_VALUE_EXISTS;
537 for( i=0; attr->a_vals[i].bv_val; i++ ) {
540 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
545 attr->a_vals = ch_realloc( attr->a_vals,
546 sizeof( struct berval ) * (i+j) );
548 /* should check for duplicates */
550 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
551 sizeof( struct berval ) * j );
553 /* trim the mods array */
554 ch_free( mods->sml_bvalues );
555 mods->sml_bvalues = NULL;
559 snprintf( textbuf, textlen,
560 "attribute '%s' provided more than once",
561 mods->sml_desc->ad_cname.bv_val );
562 return LDAP_TYPE_OR_VALUE_EXISTS;
566 if( mods->sml_bvalues[1].bv_val != NULL ) {
567 /* check for duplicates */
569 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
571 /* check if the values we're adding already exist */
572 if( mr == NULL || !mr->smr_match ) {
573 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
574 /* test asserted values against themselves */
575 for( j = 0; j < i; j++ ) {
576 if ( bvmatch( &mods->sml_bvalues[i],
577 &mods->sml_bvalues[j] ) ) {
578 /* value exists already */
579 snprintf( textbuf, textlen,
580 "%s: value #%d provided more than once",
581 mods->sml_desc->ad_cname.bv_val, j );
582 return LDAP_TYPE_OR_VALUE_EXISTS;
589 const char *text = NULL;
590 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
592 rc = modify_check_duplicates( mods->sml_desc, mr,
593 NULL, mods->sml_bvalues, 0,
594 &text, textbuf, sizeof( textbuf ) );
596 if ( rc != LDAP_SUCCESS ) {
602 attr = ch_calloc( 1, sizeof(Attribute) );
604 /* move ad to attr structure */
605 attr->a_desc = mods->sml_desc;
606 mods->sml_desc = NULL;
608 /* move values to attr structure */
609 /* should check for duplicates */
610 attr->a_vals = mods->sml_bvalues;
611 mods->sml_bvalues = NULL;
614 tail = &attr->a_next;
621 static Slapi_PBlock *initAddPlugin( Backend *be, Connection *conn, Operation *op,
622 struct berval *dn, Entry *e, int manageDSAit )
628 slapi_x_backend_set_pb( pb, be );
629 slapi_x_connection_set_pb( pb, conn );
630 slapi_x_operation_set_pb( pb, op );
632 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
633 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
634 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
639 static int doPreAddPluginFNs( Backend *be, Slapi_PBlock *pb )
643 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
646 * A preoperation plugin failure will abort the
650 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
653 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
655 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
665 static void doPostAddPluginFNs( Backend *be, Slapi_PBlock *pb )
669 rc = doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb );
672 LDAP_LOG( OPERATION, INFO, "do_add: add postoperation plugin failed\n",
675 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
680 #endif /* LDAP_SLAPI */