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;
58 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
60 * Parse the add request. It looks like this:
62 * AddRequest := [APPLICATION 14] SEQUENCE {
63 * name DistinguishedName,
64 * attrs SEQUENCE OF SEQUENCE {
66 * values SET OF AttributeValue
72 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
73 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
74 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
75 return SLAPD_DISCONNECT;
78 e = (Entry *) ch_calloc( 1, sizeof(Entry) );
80 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
83 if( rs->sr_err != LDAP_SUCCESS ) {
84 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
85 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
89 ber_dupbv( &e->e_name, &op->o_req_dn );
90 ber_dupbv( &e->e_nname, &op->o_req_ndn );
92 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", e->e_dn, 0, 0 );
95 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
96 tag = ber_next_element( ber, &len, last ) )
101 tmp.sml_nvalues = NULL;
103 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
105 if ( rtag == LBER_ERROR ) {
106 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
107 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
108 rs->sr_err = SLAPD_DISCONNECT;
112 if ( tmp.sml_values == NULL ) {
113 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
114 tmp.sml_type.bv_val, 0, 0 );
115 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
116 "no values for attribute type" );
120 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
121 mod->sml_op = LDAP_MOD_ADD;
122 mod->sml_next = NULL;
123 mod->sml_desc = NULL;
124 mod->sml_type = tmp.sml_type;
125 mod->sml_values = tmp.sml_values;
126 mod->sml_nvalues = NULL;
129 modtail = &mod->sml_next;
132 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
133 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
134 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
135 rs->sr_err = SLAPD_DISCONNECT;
139 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
140 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
144 if ( modlist == NULL ) {
145 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
146 "no attributes provided" );
150 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu ADD dn=\"%s\"\n",
151 op->o_connid, op->o_opid, e->e_name.bv_val, 0, 0 );
153 if( e->e_nname.bv_len == 0 ) {
154 /* protocolError may be a more appropriate error */
155 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
156 "root DSE already exists" );
159 } else if ( bvmatch( &e->e_nname, &frontendDB->be_schemandn ) ) {
160 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
161 "subschema subentry already exists" );
165 /* temporary; remove if not invoking backend function */
167 op->ora_modlist = modlist;
169 op->o_bd = frontendDB;
170 rs->sr_err = frontendDB->be_add( op, rs );
171 if ( rs->sr_err == 0 ) {
176 slap_graduate_commit_csn( op );
178 if( modlist != NULL ) {
179 slap_mods_free( modlist );
184 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
185 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
191 fe_op_add( Operation *op, SlapReply *rs )
194 Entry *e = op->ora_e;
195 Modifications *modlist = op->ora_modlist;
196 Modifications **modtail = &modlist;
198 manageDSAit = get_manageDSAit( op );
201 * We could be serving multiple database backends. Select the
202 * appropriate one, or send a referral to our "referral server"
203 * if we don't hold it.
205 op->o_bd = select_backend( &e->e_nname, manageDSAit, 0 );
206 if ( op->o_bd == NULL ) {
207 rs->sr_ref = referral_rewrite( default_referral,
208 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
209 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
211 rs->sr_err = LDAP_REFERRAL;
212 send_ldap_result( op, rs );
214 if ( rs->sr_ref != default_referral ) {
215 ber_bvarray_free( rs->sr_ref );
218 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
219 "no global superior knowledge" );
224 /* check restrictions */
225 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
226 send_ldap_result( op, rs );
230 /* check for referrals */
231 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
236 if ( op->o_pb ) init_add_pblock( op, &op->o_req_dn, e, manageDSAit );
237 #endif /* LDAP_SLAPI */
240 * do the add if 1 && (2 || 3)
241 * 1) there is an add function implemented in this backend;
242 * 2) this backend is master for what it holds;
243 * 3) it's a replica and the dn supplied is the updatedn.
245 if ( op->o_bd->be_add ) {
246 /* do the update here */
247 int repl_user = be_isupdate( op );
248 #ifndef SLAPD_MULTIMASTER
249 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
252 int update = op->o_bd->be_update_ndn.bv_len;
253 char textbuf[SLAP_TEXT_BUFLEN];
254 size_t textlen = sizeof textbuf;
255 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
257 rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
258 textbuf, textlen, NULL );
260 if( rs->sr_err != LDAP_SUCCESS ) {
261 send_ldap_result( op, rs );
266 for( modtail = &modlist;
268 modtail = &(*modtail)->sml_next )
270 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
271 assert( (*modtail)->sml_desc != NULL );
273 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
274 &rs->sr_text, textbuf, textlen, 1 );
275 if( rs->sr_err != LDAP_SUCCESS ) {
276 send_ldap_result( op, rs );
281 rs->sr_err = slap_mods2entry( modlist, &e, repl_user, 0,
282 &rs->sr_text, textbuf, textlen );
283 if( rs->sr_err != LDAP_SUCCESS ) {
284 send_ldap_result( op, rs );
290 * Call the preoperation plugin here, because the entry
291 * will actually contain something.
294 rs->sr_err = call_add_preop_plugins( op );
295 if ( rs->sr_err != LDAP_SUCCESS ) {
296 /* plugin will have sent result */
300 #endif /* LDAP_SLAPI */
303 #ifdef SLAPD_MULTIMASTER
307 cb.sc_next = op->o_callback;
308 op->o_callback = &cb;
310 if ( (op->o_bd->be_add)( op, rs ) == 0 ) {
311 be_entry_release_w( op, e );
315 #ifndef SLAPD_MULTIMASTER
317 BerVarray defref = NULL;
320 * SLAPI_ADD_ENTRY will be empty, but this may be acceptable
321 * on replicas (for now, it involves the minimum code intrusion).
324 rs->sr_err = call_add_preop_plugins( op );
325 if ( rs->sr_err != LDAP_SUCCESS ) {
326 /* plugin will have sent result */
330 #endif /* LDAP_SLAPI */
332 defref = op->o_bd->be_update_refs
333 ? op->o_bd->be_update_refs : default_referral;
335 if ( defref != NULL ) {
336 rs->sr_ref = referral_rewrite( defref,
337 NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
338 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
339 rs->sr_err = LDAP_REFERRAL;
340 if (!rs->sr_ref) rs->sr_ref = default_referral;
341 send_ldap_result( op, rs );
343 if ( rs->sr_ref != default_referral ) {
344 ber_bvarray_free( rs->sr_ref );
347 send_ldap_error( op, rs,
348 LDAP_UNWILLING_TO_PERFORM,
349 "shadow context; no update referral" );
351 #endif /* SLAPD_MULTIMASTER */
356 rs->sr_err = call_add_preop_plugins( op );
357 if ( rs->sr_err != LDAP_SUCCESS ) {
358 /* plugin will have sent result */
363 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
364 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
365 "operation not supported within namingContext" );
369 if ( op->o_pb ) call_add_postop_plugins( op );
370 #endif /* LDAP_SLAPI */
383 char *textbuf, size_t textlen )
385 Attribute **tail = &(*e)->e_attrs;
386 assert( *tail == NULL );
390 for( ; mods != NULL; mods = mods->sml_next ) {
394 assert( mods->sml_op == LDAP_MOD_ADD );
396 assert( mods->sml_desc != NULL );
398 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
401 #define SLURPD_FRIENDLY
402 #ifdef SLURPD_FRIENDLY
406 snprintf( textbuf, textlen,
407 "attribute '%s' provided more than once",
408 mods->sml_desc->ad_cname.bv_val );
409 return LDAP_TYPE_OR_VALUE_EXISTS;
412 for( i=0; attr->a_vals[i].bv_val; i++ ) {
415 for( j=0; mods->sml_values[j].bv_val; j++ ) {
420 attr->a_vals = ch_realloc( attr->a_vals,
421 sizeof( struct berval ) * (i+j) );
423 /* should check for duplicates */
425 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
426 sizeof( struct berval ) * j );
428 /* trim the mods array */
429 ch_free( mods->sml_values );
430 mods->sml_values = NULL;
432 if( mods->sml_nvalues ) {
433 attr->a_nvals = ch_realloc( attr->a_nvals,
434 sizeof( struct berval ) * (i+j) );
436 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
437 sizeof( struct berval ) * j );
439 /* trim the mods array */
440 ch_free( mods->sml_nvalues );
441 mods->sml_nvalues = NULL;
444 attr->a_nvals = attr->a_vals;
449 snprintf( textbuf, textlen,
450 "attribute '%s' provided more than once",
451 mods->sml_desc->ad_cname.bv_val );
452 return LDAP_TYPE_OR_VALUE_EXISTS;
456 if( mods->sml_values[1].bv_val != NULL ) {
457 /* check for duplicates */
459 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
461 /* check if the values we're adding already exist */
462 if( mr == NULL || !mr->smr_match ) {
463 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
464 /* test asserted values against themselves */
465 for( j = 0; j < i; j++ ) {
466 if ( bvmatch( &mods->sml_values[i],
467 &mods->sml_values[j] ) )
469 /* value exists already */
470 snprintf( textbuf, textlen,
471 "%s: value #%d provided more than once",
472 mods->sml_desc->ad_cname.bv_val, j );
473 return LDAP_TYPE_OR_VALUE_EXISTS;
482 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
483 /* test asserted values against themselves */
484 for( j = 0; j < i; j++ ) {
485 rc = value_match( &match, mods->sml_desc, mr,
487 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
488 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
489 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
491 ? &mods->sml_nvalues[i]
492 : &mods->sml_values[i],
494 ? &mods->sml_nvalues[j]
495 : &mods->sml_values[j],
498 if ( rc == LDAP_SUCCESS && match == 0 ) {
499 /* value exists already */
500 snprintf( textbuf, textlen,
501 "%s: value #%d provided more than once",
502 mods->sml_desc->ad_cname.bv_val, j );
503 return LDAP_TYPE_OR_VALUE_EXISTS;
505 } else if ( rc != LDAP_SUCCESS ) {
513 attr = ch_calloc( 1, sizeof(Attribute) );
515 /* move ad to attr structure */
516 attr->a_desc = mods->sml_desc;
517 if ( !dup ) mods->sml_desc = NULL;
519 /* move values to attr structure */
520 /* should check for duplicates */
523 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
524 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
525 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
526 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
528 BER_BVZERO( &attr->a_vals[i] );
530 attr->a_vals = mods->sml_values;
531 mods->sml_values = NULL;
534 if ( mods->sml_nvalues ) {
537 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
538 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
539 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
540 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
542 BER_BVZERO( &attr->a_nvals[i] );
544 attr->a_nvals = mods->sml_nvalues;
545 mods->sml_nvalues = NULL;
548 attr->a_nvals = attr->a_vals;
552 tail = &attr->a_next;
561 Modifications **mods,
563 char *textbuf, size_t textlen )
565 Modifications *modhead = NULL;
567 Modifications **modtail = &modhead;
569 AttributeDescription *a_new_desc;
574 while ( a_new != NULL ) {
575 a_new_desc = a_new->a_desc;
576 mod = (Modifications *) malloc( sizeof( Modifications ));
578 mod->sml_op = LDAP_MOD_REPLACE;
580 mod->sml_type = a_new_desc->ad_cname;
582 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
584 mod->sml_values = (struct berval*) malloc(
585 (count+1) * sizeof( struct berval) );
587 /* see slap_mods_check() comments...
588 * if a_vals == a_nvals, there is no normalizer.
589 * in this case, mod->sml_nvalues must be left NULL.
591 if ( a_new->a_vals != a_new->a_nvals ) {
592 mod->sml_nvalues = (struct berval*) malloc(
593 (count+1) * sizeof( struct berval) );
595 mod->sml_nvalues = NULL;
598 for ( i = 0; i < count; i++ ) {
599 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
600 if ( mod->sml_nvalues ) {
601 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
605 mod->sml_values[count].bv_val = NULL;
606 mod->sml_values[count].bv_len = 0;
608 if ( mod->sml_nvalues ) {
609 mod->sml_nvalues[count].bv_val = NULL;
610 mod->sml_nvalues[count].bv_len = 0;
613 mod->sml_desc = a_new_desc;
616 modtail = &mod->sml_next;
617 a_new = a_new->a_next;
626 static void init_add_pblock( Operation *op,
627 struct berval *dn, Entry *e, int manageDSAit )
629 slapi_int_pblock_set_operation( op->o_pb, op );
630 slapi_pblock_set( op->o_pb, SLAPI_ADD_TARGET, (void *)dn->bv_val );
631 slapi_pblock_set( op->o_pb, SLAPI_ADD_ENTRY, (void *)e );
632 slapi_pblock_set( op->o_pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
635 static int call_add_preop_plugins( Operation *op )
639 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_PRE_ADD_FN, op->o_pb );
642 * A preoperation plugin failure will abort the
645 Debug(LDAP_DEBUG_TRACE,
646 "do_add: add preoperation plugin failed.\n",
649 if (( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
650 (void *)&rc ) != 0 ) || rc == LDAP_SUCCESS )
661 static void call_add_postop_plugins( Operation *op )
665 rc = slapi_int_call_plugins( op->o_bd, SLAPI_PLUGIN_POST_ADD_FN, op->o_pb );
667 Debug(LDAP_DEBUG_TRACE,
668 "do_add: add postoperation plugin failed\n",
672 #endif /* LDAP_SLAPI */