2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms are permitted
19 * provided that this notice is preserved and that due credit is given
20 * to the University of Michigan at Ann Arbor. The name of the University
21 * may not be used to endorse or promote products derived from this
22 * software without specific prior written permission. This software
23 * is provided ``as is'' without express or implied warranty.
29 #include <ac/string.h>
31 #include <ac/socket.h>
37 #include "slapi/slapi.h"
39 static void init_add_pblock( Operation *op, struct berval *dn, Entry *e,
41 static int call_add_preop_plugins( Operation *op );
42 static void call_add_postop_plugins( Operation *op );
43 #endif /* LDAP_SLAPI */
46 do_add( Operation *op, SlapReply *rs )
48 BerElement *ber = op->o_ber;
50 struct berval dn = BER_BVNULL;
54 Modifications *modlist = NULL;
55 Modifications **modtail = &modlist;
59 LDAP_LOG( OPERATION, ENTRY, "do_add: conn %d enter\n", op->o_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", op->o_connid,0,0 );
81 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
83 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
84 return SLAPD_DISCONNECT;
87 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
89 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
92 if( rs->sr_err != LDAP_SUCCESS ) {
94 LDAP_LOG( OPERATION, ERR,
95 "do_add: conn %d invalid dn (%s)\n", op->o_connid, dn.bv_val, 0 );
97 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
99 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
103 ber_dupbv( &e->e_name, &op->o_req_dn );
104 ber_dupbv( &e->e_nname, &op->o_req_ndn );
107 LDAP_LOG( OPERATION, ARGS,
108 "do_add: conn %d dn (%s)\n", op->o_connid, e->e_dn, 0 );
110 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
114 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
115 tag = ber_next_element( ber, &len, last ) )
120 tmp.sml_nvalues = NULL;
122 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
124 if ( rtag == LBER_ERROR ) {
126 LDAP_LOG( OPERATION, ERR,
127 "do_add: conn %d decoding error \n", op->o_connid, 0, 0 );
129 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
131 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
132 rs->sr_err = SLAPD_DISCONNECT;
136 if ( tmp.sml_values == NULL ) {
138 LDAP_LOG( OPERATION, INFO,
139 "do_add: conn %d no values for type %s\n",
140 op->o_connid, tmp.sml_type.bv_val, 0 );
142 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
143 tmp.sml_type.bv_val, 0, 0 );
145 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
146 "no values for attribute type" );
150 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
151 mod->sml_op = LDAP_MOD_ADD;
152 mod->sml_next = NULL;
153 mod->sml_desc = NULL;
154 mod->sml_type = tmp.sml_type;
155 mod->sml_values = tmp.sml_values;
156 mod->sml_nvalues = NULL;
159 modtail = &mod->sml_next;
162 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
164 LDAP_LOG( OPERATION, ERR,
165 "do_add: conn %d ber_scanf failed\n", op->o_connid, 0, 0 );
167 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
169 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
170 rs->sr_err = SLAPD_DISCONNECT;
174 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
176 LDAP_LOG( OPERATION, INFO,
177 "do_add: conn %d get_ctrls failed\n", op->o_connid, 0, 0 );
179 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
184 if ( modlist == NULL ) {
185 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
186 "no attributes provided" );
190 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
191 op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
193 if( e->e_nname.bv_len == 0 ) {
194 /* protocolError may be a more appropriate error */
195 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
196 "root DSE already exists" );
199 } else if ( bvmatch( &e->e_nname, &frontendDB->be_schemandn ) ) {
200 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
201 "subschema subentry already exists" );
205 /* temporary; remove if not invoking backend function */
207 op->ora_modlist = modlist;
209 op->o_bd = frontendDB;
210 rs->sr_err = frontendDB->be_add( op, rs );
211 if ( rs->sr_err == 0 ) {
216 slap_graduate_commit_csn( op );
218 if( modlist != NULL ) {
219 slap_mods_free( modlist );
224 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
225 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
231 fe_op_add( Operation *op, SlapReply *rs )
234 Entry *e = op->ora_e;
235 Modifications *modlist = op->ora_modlist;
236 Modifications **modtail = &modlist;
238 manageDSAit = get_manageDSAit( op );
241 * We could be serving multiple database backends. Select the
242 * appropriate one, or send a referral to our "referral server"
243 * if we don't hold it.
245 op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
246 if ( op->o_bd == NULL ) {
247 rs->sr_ref = referral_rewrite( default_referral,
248 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
249 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
251 rs->sr_err = LDAP_REFERRAL;
252 send_ldap_result( op, rs );
254 if ( rs->sr_ref != default_referral ) {
255 ber_bvarray_free( rs->sr_ref );
258 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
259 "no global superior knowledge" );
264 /* check restrictions */
265 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
266 send_ldap_result( op, rs );
270 /* check for referrals */
271 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
276 if ( op->o_pb ) init_add_pblock( op, &op->o_req_dn, e, manageDSAit );
277 #endif /* LDAP_SLAPI */
280 * do the add if 1 && (2 || 3)
281 * 1) there is an add function implemented in this backend;
282 * 2) this backend is master for what it holds;
283 * 3) it's a replica and the dn supplied is the updatedn.
285 if ( op->o_bd->be_add ) {
286 /* do the update here */
287 int repl_user = be_isupdate( op );
288 #ifndef SLAPD_MULTIMASTER
289 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
292 int update = op->o_bd->be_update_ndn.bv_len;
293 char textbuf[SLAP_TEXT_BUFLEN];
294 size_t textlen = sizeof textbuf;
295 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
297 rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
298 textbuf, textlen, NULL );
300 if( rs->sr_err != LDAP_SUCCESS ) {
301 send_ldap_result( op, rs );
306 for( modtail = &modlist;
308 modtail = &(*modtail)->sml_next )
310 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
311 assert( (*modtail)->sml_desc != NULL );
313 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
314 &rs->sr_text, textbuf, textlen );
315 if( rs->sr_err != LDAP_SUCCESS ) {
316 send_ldap_result( op, rs );
321 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
322 &rs->sr_text, textbuf, textlen );
323 if( rs->sr_err != LDAP_SUCCESS ) {
324 send_ldap_result( op, rs );
330 * Call the preoperation plugin here, because the entry
331 * will actually contain something.
334 rs->sr_err = call_add_preop_plugins( op );
335 if ( rs->sr_err != LDAP_SUCCESS ) {
336 /* plugin will have sent result */
340 #endif /* LDAP_SLAPI */
343 #ifdef SLAPD_MULTIMASTER
347 cb.sc_next = op->o_callback;
348 op->o_callback = &cb;
350 if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
351 be_entry_release_w( op, e );
355 #ifndef SLAPD_MULTIMASTER
357 BerVarray defref = NULL;
360 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
361 * on replicas (for now, it involves the minimum code intrusion).
364 rs->sr_err = call_add_preop_plugins( op );
365 if ( rs->sr_err != LDAP_SUCCESS ) {
366 /* plugin will have sent result */
370 #endif /* LDAP_SLAPI */
372 defref = op->o_bd->be_update_refs
373 ? op->o_bd->be_update_refs : default_referral;
375 if ( defref != NULL ) {
376 rs->sr_ref = referral_rewrite( defref,
377 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
378 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
379 rs->sr_err = LDAP_REFERRAL;
380 if (!rs->sr_ref) rs->sr_ref = default_referral;
381 send_ldap_result( op, rs );
383 if ( rs->sr_ref != default_referral ) {
384 ber_bvarray_free( rs->sr_ref );
387 send_ldap_error( op, rs,
388 LDAP_UNWILLING_TO_PERFORM,
389 "shadow context; no update referral" );
391 #endif /* SLAPD_MULTIMASTER */
396 rs->sr_err = call_add_preop_plugins( op );
397 if ( rs->sr_err != LDAP_SUCCESS ) {
398 /* plugin will have sent result */
404 LDAP_LOG( OPERATION, INFO,
405 "do_add: conn %d no backend support\n", op->o_connid, 0, 0 );
407 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
409 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
410 "operation not supported within namingContext" );
414 if ( op->o_pb ) call_add_postop_plugins( op );
415 #endif /* LDAP_SLAPI */
428 char *textbuf, size_t textlen )
430 Attribute **tail = &(*e)->e_attrs;
431 assert( *tail == NULL );
435 for( ; mods != NULL; mods = mods->sml_next ) {
439 assert( mods->sml_op == LDAP_MOD_ADD );
441 assert( mods->sml_desc != NULL );
443 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
446 #define SLURPD_FRIENDLY
447 #ifdef SLURPD_FRIENDLY
451 snprintf( textbuf, textlen,
452 "attribute '%s' provided more than once",
453 mods->sml_desc->ad_cname.bv_val );
454 return LDAP_TYPE_OR_VALUE_EXISTS;
457 for( i=0; attr->a_vals[i].bv_val; i++ ) {
460 for( j=0; mods->sml_values[j].bv_val; j++ ) {
465 attr->a_vals = ch_realloc( attr->a_vals,
466 sizeof( struct berval ) * (i+j) );
468 /* should check for duplicates */
470 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
471 sizeof( struct berval ) * j );
473 /* trim the mods array */
474 ch_free( mods->sml_values );
475 mods->sml_values = NULL;
477 if( mods->sml_nvalues ) {
478 attr->a_nvals = ch_realloc( attr->a_nvals,
479 sizeof( struct berval ) * (i+j) );
481 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
482 sizeof( struct berval ) * j );
484 /* trim the mods array */
485 ch_free( mods->sml_nvalues );
486 mods->sml_nvalues = NULL;
489 attr->a_nvals = attr->a_vals;
494 snprintf( textbuf, textlen,
495 "attribute '%s' provided more than once",
496 mods->sml_desc->ad_cname.bv_val );
497 return LDAP_TYPE_OR_VALUE_EXISTS;
501 if( mods->sml_values[1].bv_val != NULL ) {
502 /* check for duplicates */
504 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
506 /* check if the values we're adding already exist */
507 if( mr == NULL || !mr->smr_match ) {
508 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
509 /* test asserted values against themselves */
510 for( j = 0; j < i; j++ ) {
511 if ( bvmatch( &mods->sml_values[i],
512 &mods->sml_values[j] ) )
514 /* value exists already */
515 snprintf( textbuf, textlen,
516 "%s: value #%d provided more than once",
517 mods->sml_desc->ad_cname.bv_val, j );
518 return LDAP_TYPE_OR_VALUE_EXISTS;
527 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
528 /* test asserted values against themselves */
529 for( j = 0; j < i; j++ ) {
530 rc = value_match( &match, mods->sml_desc, mr,
532 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
533 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
534 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
536 ? &mods->sml_nvalues[i]
537 : &mods->sml_values[i],
539 ? &mods->sml_nvalues[j]
540 : &mods->sml_values[j],
543 if ( rc == LDAP_SUCCESS && match == 0 ) {
544 /* value exists already */
545 snprintf( textbuf, textlen,
546 "%s: value #%d provided more than once",
547 mods->sml_desc->ad_cname.bv_val, j );
548 return LDAP_TYPE_OR_VALUE_EXISTS;
550 } else if ( rc != LDAP_SUCCESS ) {
558 attr = ch_calloc( 1, sizeof(Attribute) );
560 /* move ad to attr structure */
561 attr->a_desc = mods->sml_desc;
562 if ( !dup ) mods->sml_desc = NULL;
564 /* move values to attr structure */
565 /* should check for duplicates */
568 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
569 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
570 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
571 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
573 attr->a_vals[i].bv_len = 0;
574 attr->a_vals[i].bv_val = NULL;
576 attr->a_vals = mods->sml_values;
577 mods->sml_values = NULL;
580 if ( mods->sml_nvalues ) {
583 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
584 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
585 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
586 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
588 attr->a_nvals[i].bv_len = 0;
589 attr->a_nvals[i].bv_val = NULL;
591 attr->a_nvals = mods->sml_nvalues;
592 mods->sml_nvalues = NULL;
595 attr->a_nvals = attr->a_vals;
599 tail = &attr->a_next;
608 Modifications **mods,
610 char *textbuf, size_t textlen )
612 Modifications *modhead = NULL;
614 Modifications **modtail = &modhead;
616 AttributeDescription *a_new_desc;
621 while ( a_new != NULL ) {
622 a_new_desc = a_new->a_desc;
623 mod = (Modifications *) malloc( sizeof( Modifications ));
625 mod->sml_op = LDAP_MOD_REPLACE;
627 mod->sml_type = a_new_desc->ad_cname;
629 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
631 mod->sml_values = (struct berval*) malloc(
632 (count+1) * sizeof( struct berval) );
634 /* see slap_mods_check() comments...
635 * if a_vals == a_nvals, there is no normalizer.
636 * in this case, mod->sml_nvalues must be left NULL.
638 if ( a_new->a_vals != a_new->a_nvals ) {
639 mod->sml_nvalues = (struct berval*) malloc(
640 (count+1) * sizeof( struct berval) );
642 mod->sml_nvalues = NULL;
645 for ( i = 0; i < count; i++ ) {
646 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
647 if ( mod->sml_nvalues ) {
648 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
652 mod->sml_values[count].bv_val = NULL;
653 mod->sml_values[count].bv_len = 0;
655 if ( mod->sml_nvalues ) {
656 mod->sml_nvalues[count].bv_val = NULL;
657 mod->sml_nvalues[count].bv_len = 0;
660 mod->sml_desc = a_new_desc;
663 modtail = &mod->sml_next;
664 a_new = a_new->a_next;
673 static void init_add_pblock( Operation *op,
674 struct berval *dn, Entry *e, int manageDSAit )
676 slapi_int_pblock_set_operation( op->o_pb, op );
677 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
678 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
679 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
682 static int call_add_preop_plugins( Operation *op )
686 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
689 * A preoperation plugin failure will abort the
693 LDAP_LOG( OPERATION, INFO,
694 "do_add: add preoperation plugin failed\n",
697 Debug(LDAP_DEBUG_TRACE,
698 "do_add: add preoperation plugin failed.\n",
702 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
703 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
714 static void call_add_postop_plugins( Operation *op )
718 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
721 LDAP_LOG( OPERATION, INFO,
722 "do_add: add postoperation plugin failed\n",
725 Debug(LDAP_DEBUG_TRACE,
726 "do_add: add postoperation plugin failed\n",
731 #endif /* LDAP_SLAPI */