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 do_add( Connection *conn, Operation *op )
32 BerElement *ber = op->o_ber;
34 struct berval dn = { 0, NULL };
39 Modifications *modlist = NULL;
40 Modifications **modtail = &modlist;
41 Modifications tmp, *mod;
45 int rc = LDAP_SUCCESS;
48 Slapi_PBlock *pb = op->o_pb;
51 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", conn->c_connid,0,0 );
53 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
56 * Parse the add request. It looks like this:
58 * AddRequest := [APPLICATION 14] SEQUENCE {
59 * name DistinguishedName,
60 * attrs SEQUENCE OF SEQUENCE {
62 * values SET OF AttributeValue
68 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
70 LDAP_LOG( OPERATION, ERR,
71 "do_add: conn %d ber_scanf failed\n", conn->c_connid,0,0 );
73 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
75 send_ldap_disconnect( conn, op,
76 LDAP_PROTOCOL_ERROR, "decoding error" );
80 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
82 rc = dnPrettyNormal( NULL, &dn, &e->e_name, &e->e_nname );
84 if( rc != LDAP_SUCCESS ) {
86 LDAP_LOG( OPERATION, ERR,
87 "do_add: conn %d invalid dn (%s)\n", conn->c_connid, dn.bv_val, 0 );
89 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
91 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
92 "invalid DN", NULL, NULL );
97 LDAP_LOG( OPERATION, ARGS,
98 "do_add: conn %d dn (%s)\n", conn->c_connid, e->e_dn, 0 );
100 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
104 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
105 tag = ber_next_element( ber, &len, last ) )
110 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_bvalues );
112 if ( rtag == LBER_ERROR ) {
114 LDAP_LOG( OPERATION, ERR,
115 "do_add: conn %d decoding error \n", conn->c_connid, 0, 0 );
117 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
119 send_ldap_disconnect( conn, op,
120 LDAP_PROTOCOL_ERROR, "decoding error" );
125 if ( tmp.sml_bvalues == NULL ) {
127 LDAP_LOG( OPERATION, INFO,
128 "do_add: conn %d no values for type %s\n",
129 conn->c_connid, tmp.sml_type.bv_val, 0 );
131 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
132 tmp.sml_type.bv_val, 0, 0 );
134 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
135 NULL, "no values for attribute type", NULL, NULL );
138 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
140 mod->sml_op = LDAP_MOD_ADD;
141 mod->sml_next = NULL;
142 mod->sml_desc = NULL;
143 mod->sml_type = tmp.sml_type;
144 mod->sml_bvalues = tmp.sml_bvalues;
147 modtail = &mod->sml_next;
150 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
152 LDAP_LOG( OPERATION, ERR,
153 "do_add: conn %d ber_scanf failed\n", conn->c_connid, 0, 0 );
155 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
157 send_ldap_disconnect( conn, op,
158 LDAP_PROTOCOL_ERROR, "decoding error" );
163 if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
165 LDAP_LOG( OPERATION, INFO,
166 "do_add: conn %d get_ctrls failed\n", conn->c_connid, 0, 0 );
168 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
173 if ( modlist == NULL ) {
174 send_ldap_result( conn, op, rc = LDAP_PROTOCOL_ERROR,
175 NULL, "no attributes provided", NULL, NULL );
179 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
180 op->o_connid, op->o_opid, e->e_dn, 0, 0 );
182 if( e->e_nname.bv_len == 0 ) {
183 /* protocolError may be a more appropriate error */
184 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
185 NULL, "root DSE already exists",
189 } else if ( bvmatch( &e->e_nname, &global_schemandn ) ) {
190 send_ldap_result( conn, op, rc = LDAP_ALREADY_EXISTS,
191 NULL, "subschema subentry already exists",
197 * is objectClass special?
200 /* look for objectClass attribute */
201 for ( mod = modlist; mod; mod = mod->sml_next ) {
202 AttributeDescription *mod_desc = NULL;
204 rc = slap_bv2ad( &mod->sml_type, &mod_desc, &text );
205 if ( rc != LDAP_SUCCESS ) {
206 send_ldap_result( conn, op, rc,
207 NULL, text, NULL, NULL );
211 if ( mod_desc == slap_schema.si_ad_objectClass ) {
217 send_ldap_result( conn, op,
218 rc = LDAP_OBJECT_CLASS_VIOLATION,
219 NULL, "objectClass missing",
224 /* look for special objectClass */
225 for ( cnt = 0; mod->sml_bvalues[ cnt ].bv_val; cnt++ ) {
228 oc = oc_bvfind( &mod->sml_bvalues[ cnt ] );
231 send_ldap_result( conn, op,
232 rc = LDAP_OBJECT_CLASS_VIOLATION,
233 NULL, "undefined objectClass",
238 /* check for special objectClass */
239 if ( oc == slap_schema.si_oc_alias ) {
243 if ( oc == slap_schema.si_oc_referral ) {
247 if ( oc == slap_schema.si_oc_extensibleObject ) {
255 if ( mod->sml_bvalues[ cnt ].bv_val == NULL ) {
258 * Get attribute type(s) and attribute value(s) of our rdn,
260 if ( ldap_bv2rdn( &e->e_name, &rdn, (char **)&text,
261 LDAP_DN_FORMAT_LDAP ) )
263 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX,
264 NULL, "unknown type(s) used in RDN",
269 /* Check for RDN attrs in entry */
270 for ( cnt = 0; rdn[ 0 ][ cnt ]; cnt++ ) {
271 AttributeDescription *desc = NULL;
275 rc = slap_bv2ad( &rdn[ 0 ][ cnt ]->la_attr,
278 if ( rc != LDAP_SUCCESS ) {
279 send_ldap_result( conn, op, rc,
280 NULL, text, NULL, NULL );
284 for (mod = modlist; mod; mod = mod->sml_next) {
285 AttributeDescription *mod_desc = NULL;
287 rc = slap_bv2ad( &mod->sml_type,
289 if ( rc != LDAP_SUCCESS ) {
290 send_ldap_result( conn, op, rc,
291 NULL, text, NULL, NULL );
295 if (mod_desc == desc) {
306 send_ldap_result( conn, op,
307 rc = LDAP_NO_SUCH_ATTRIBUTE,
309 "attribute in RDN not listed in entry",
313 #else /* ! BAILOUT */
314 /* add attribute type and value to modlist */
315 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
317 mod->sml_op = LDAP_MOD_ADD;
318 mod->sml_next = NULL;
319 mod->sml_desc = NULL;
321 ber_dupbv( &mod->sml_type,
322 &rdn[ 0 ][ cnt ]->la_attr );
324 mod->sml_bvalues = NULL;
325 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
326 ber_bvarray_add( &mod->sml_bvalues, &bv );
329 modtail = &mod->sml_next;
331 #endif /* ! BAILOUT */
334 mr = desc->ad_type->sat_equality;
335 if (mr == NULL || !mr->smr_match ) {
337 send_ldap_result( conn, op,
338 rc = LDAP_INVALID_SYNTAX,
340 "attribute in RDN lacks matching rule",
345 for (i = 0; mod->sml_bvalues[ i ].bv_val; i++) {
348 rc = value_match(&match, desc, mr,
349 SLAP_MR_VALUE_SYNTAX_MATCH,
350 &mod->sml_bvalues[ i ],
351 &rdn[ 0 ][ cnt ]->la_value, &text);
353 if ( rc != LDAP_SUCCESS ) {
354 send_ldap_result( conn, op, rc,
355 NULL, text, NULL, NULL);
365 if (mod->sml_bvalues[ i ].bv_val == NULL) {
368 send_ldap_result( conn, op,
369 rc = LDAP_NO_SUCH_ATTRIBUTE,
371 "value in RDN not listed in entry",
375 #else /* ! BAILOUT */
378 /* add attribute type and value to modlist */
379 ber_dupbv( &bv, &rdn[ 0 ][ cnt ]->la_value );
380 ber_bvarray_add( &mod->sml_bvalues, &bv );
382 #endif /* ! BAILOUT */
387 manageDSAit = get_manageDSAit( op );
390 * We could be serving multiple database backends. Select the
391 * appropriate one, or send a referral to our "referral server"
392 * if we don't hold it.
394 be = select_backend( &e->e_nname, manageDSAit, 0 );
396 BerVarray ref = referral_rewrite( default_referral,
397 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
399 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
400 NULL, NULL, ref ? ref : default_referral, NULL );
402 if ( ref ) ber_bvarray_free( ref );
406 /* check restrictions */
407 rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
408 if( rc != LDAP_SUCCESS ) {
409 send_ldap_result( conn, op, rc,
410 NULL, text, NULL, NULL );
414 /* check for referrals */
415 rc = backend_check_referrals( be, conn, op, &e->e_name, &e->e_nname );
416 if ( rc != LDAP_SUCCESS ) {
420 #if defined( LDAP_SLAPI )
421 slapi_x_backend_set_pb( pb, be );
422 slapi_x_connection_set_pb( pb, conn );
423 slapi_x_operation_set_pb( pb, op );
424 slapi_pblock_set( pb, SLAPI_ADD_ENTRY, (void *)e );
425 slapi_pblock_set( pb, SLAPI_ADD_TARGET, (void *)dn.bv_val );
426 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
428 rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_ADD_FN, pb );
431 * A preoperation plugin failure will abort the
435 LDAP_LOG( OPERATION, INFO, "do_add: add preoperation plugin failed\n",
438 Debug(LDAP_DEBUG_TRACE, "do_add: add preoperation plugin failed.\n",
440 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0 )
445 #endif /* defined( 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 );
498 if ( (*be->be_add)( be, conn, op, e ) == 0 ) {
499 #ifdef SLAPD_MULTIMASTER
503 replog( be, op, &e->e_name, &e->e_nname, e );
505 be_entry_release_w( be, conn, op, e );
509 #ifndef SLAPD_MULTIMASTER
511 BerVarray defref = be->be_update_refs
512 ? be->be_update_refs : default_referral;
513 BerVarray ref = referral_rewrite( defref,
514 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
516 send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
517 ref ? ref : defref, NULL );
519 if ( ref ) ber_bvarray_free( ref );
524 LDAP_LOG( OPERATION, INFO,
525 "do_add: conn %d no backend support\n", conn->c_connid, 0, 0 );
527 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
529 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
530 NULL, "operation not supported within namingContext", NULL, NULL );
533 #if defined( LDAP_SLAPI )
535 * Postoperation errors are silently ignored; the work has
538 if ( doPluginFNs( be, SLAPI_PLUGIN_POST_ADD_FN, pb ) != 0) {
540 LDAP_LOG( OPERATION, INFO, "do_add: Add postoperation plugins failed\n",
543 Debug(LDAP_DEBUG_TRACE, "do_add: Add postoperation plugins failed.\n",
547 #endif /* defined( LDAP_SLAPI ) */
550 if( modlist != NULL ) {
551 slap_mods_free( modlist );
566 char *textbuf, size_t textlen )
568 Attribute **tail = &(*e)->e_attrs;
569 assert( *tail == NULL );
573 for( ; mods != NULL; mods = mods->sml_next ) {
576 assert( mods->sml_op == LDAP_MOD_ADD );
577 assert( mods->sml_desc != NULL );
579 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
582 #define SLURPD_FRIENDLY
583 #ifdef SLURPD_FRIENDLY
587 snprintf( textbuf, textlen,
588 "attribute '%s' provided more than once",
589 mods->sml_desc->ad_cname.bv_val );
590 return LDAP_TYPE_OR_VALUE_EXISTS;
593 for( i=0; attr->a_vals[i].bv_val; i++ ) {
596 for( j=0; mods->sml_bvalues[j].bv_val; j++ ) {
601 attr->a_vals = ch_realloc( attr->a_vals,
602 sizeof( struct berval ) * (i+j) );
604 /* should check for duplicates */
606 AC_MEMCPY( &attr->a_vals[i], mods->sml_bvalues,
607 sizeof( struct berval ) * j );
609 /* trim the mods array */
610 ch_free( mods->sml_bvalues );
611 mods->sml_bvalues = NULL;
615 snprintf( textbuf, textlen,
616 "attribute '%s' provided more than once",
617 mods->sml_desc->ad_cname.bv_val );
618 return LDAP_TYPE_OR_VALUE_EXISTS;
622 if( mods->sml_bvalues[1].bv_val != NULL ) {
623 /* check for duplicates */
625 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
627 /* check if the values we're adding already exist */
628 if( mr == NULL || !mr->smr_match ) {
629 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
630 /* test asserted values against themselves */
631 for( j = 0; j < i; j++ ) {
632 if ( bvmatch( &mods->sml_bvalues[i],
633 &mods->sml_bvalues[j] ) ) {
634 /* value exists already */
635 snprintf( textbuf, textlen,
636 "%s: value #%d provided more than once",
637 mods->sml_desc->ad_cname.bv_val, j );
638 return LDAP_TYPE_OR_VALUE_EXISTS;
645 const char *text = NULL;
646 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
648 rc = modify_check_duplicates( mods->sml_desc, mr,
649 NULL, mods->sml_bvalues, 0,
650 &text, textbuf, sizeof( textbuf ) );
652 if ( rc != LDAP_SUCCESS ) {
658 attr = ch_calloc( 1, sizeof(Attribute) );
660 /* move ad to attr structure */
661 attr->a_desc = mods->sml_desc;
662 mods->sml_desc = NULL;
664 /* move values to attr structure */
665 /* should check for duplicates */
666 attr->a_vals = mods->sml_bvalues;
667 mods->sml_bvalues = NULL;
670 tail = &attr->a_next;