2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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>
36 do_add( Operation *op, SlapReply *rs )
38 BerElement *ber = op->o_ber;
40 struct berval dn = BER_BVNULL;
43 Modifications *modlist = NULL;
44 Modifications **modtail = &modlist;
46 char textbuf[ SLAP_TEXT_BUFLEN ];
47 size_t textlen = sizeof( textbuf );
50 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
52 * Parse the add request. It looks like this:
54 * AddRequest := [APPLICATION 14] SEQUENCE {
55 * name DistinguishedName,
56 * attrs SEQUENCE OF SEQUENCE {
58 * values SET OF AttributeValue
64 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
65 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
66 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
67 return SLAPD_DISCONNECT;
70 op->ora_e = (Entry *) ch_calloc( 1, sizeof(Entry) );
72 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
75 if ( rs->sr_err != LDAP_SUCCESS ) {
76 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
77 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
81 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
82 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
84 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", op->ora_e->e_dn, 0, 0 );
87 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
88 tag = ber_next_element( ber, &len, last ) )
93 tmp.sml_nvalues = NULL;
95 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
97 if ( rtag == LBER_ERROR ) {
98 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
99 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
100 rs->sr_err = SLAPD_DISCONNECT;
104 if ( tmp.sml_values == NULL ) {
105 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
106 tmp.sml_type.bv_val, 0, 0 );
107 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
108 "no values for attribute type" );
112 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
113 mod->sml_op = LDAP_MOD_ADD;
115 mod->sml_next = NULL;
116 mod->sml_desc = NULL;
117 mod->sml_type = tmp.sml_type;
118 mod->sml_values = tmp.sml_values;
119 mod->sml_nvalues = NULL;
122 modtail = &mod->sml_next;
125 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
126 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
127 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
128 rs->sr_err = SLAPD_DISCONNECT;
132 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
133 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
137 if ( modlist == NULL ) {
138 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
139 "no attributes provided" );
143 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
144 op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
146 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
147 /* protocolError may be a more appropriate error */
148 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
149 "root DSE already exists" );
152 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
153 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
154 "subschema subentry already exists" );
158 rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
159 textbuf, textlen, NULL );
161 if ( rs->sr_err != LDAP_SUCCESS ) {
162 send_ldap_result( op, rs );
166 /* temporary; remove if not invoking backend function */
167 op->ora_modlist = modlist;
169 /* call this so global overlays/SLAPI have access to ora_e */
170 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
171 1, 0, &rs->sr_text, textbuf, textlen );
172 if ( rs->sr_err != LDAP_SUCCESS ) {
173 send_ldap_result( op, rs );
177 op->o_bd = frontendDB;
178 rc = frontendDB->be_add( op, rs );
180 if ( op->ora_e != NULL && op->o_private != NULL ) {
181 BackendDB *bd = op->o_bd;
183 op->o_bd = (BackendDB *)op->o_private;
184 op->o_private = NULL;
186 be_entry_release_w( op, op->ora_e );
190 op->o_private = NULL;
195 slap_graduate_commit_csn( op );
197 if ( modlist != NULL ) {
198 slap_mods_free( modlist );
200 if ( op->ora_e != NULL ) {
201 entry_free( op->ora_e );
203 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
204 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
210 fe_op_add( Operation *op, SlapReply *rs )
213 Modifications *modlist = op->ora_modlist;
214 Modifications **modtail = &modlist;
217 char textbuf[ SLAP_TEXT_BUFLEN ];
218 size_t textlen = sizeof( textbuf );
220 manageDSAit = get_manageDSAit( op );
223 * We could be serving multiple database backends. Select the
224 * appropriate one, or send a referral to our "referral server"
225 * if we don't hold it.
227 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
228 if ( op->o_bd == NULL ) {
229 rs->sr_ref = referral_rewrite( default_referral,
230 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
231 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
233 rs->sr_err = LDAP_REFERRAL;
234 op->o_bd = frontendDB;
235 send_ldap_result( op, rs );
238 if ( rs->sr_ref != default_referral ) {
239 ber_bvarray_free( rs->sr_ref );
242 op->o_bd = frontendDB;
243 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
244 "no global superior knowledge" );
250 /* If we've got a glued backend, check the real backend */
252 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
253 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
256 /* check restrictions */
257 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
258 send_ldap_result( op, rs );
262 /* check for referrals */
263 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
267 rs->sr_err = slap_mods_obsolete_check( op, modlist,
268 &rs->sr_text, textbuf, textlen );
270 if ( rs->sr_err != LDAP_SUCCESS ) {
271 send_ldap_result( op, rs );
276 * do the add if 1 && (2 || 3)
277 * 1) there is an add function implemented in this backend;
278 * 2) this backend is master for what it holds;
279 * 3) it's a replica and the dn supplied is the updatedn.
281 if ( op->o_bd->be_add ) {
282 /* do the update here */
283 int repl_user = be_isupdate( op );
284 #ifndef SLAPD_MULTIMASTER
285 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
288 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
289 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
294 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
295 &rs->sr_text, textbuf, textlen );
297 if ( rs->sr_err != LDAP_SUCCESS ) {
298 send_ldap_result( op, rs );
304 /* go to the last mod */
305 for ( modtail = &modlist;
307 modtail = &(*modtail)->sml_next )
309 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
310 assert( (*modtail)->sml_desc != NULL );
314 rs->sr_err = slap_mods_opattrs( op, modlist,
315 modtail, &rs->sr_text,
316 textbuf, textlen, 1 );
317 if ( rs->sr_err != LDAP_SUCCESS ) {
318 send_ldap_result( op, rs );
322 /* check for duplicate values */
323 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
324 modlist, &rs->sr_text, textbuf, textlen );
325 if ( rs->sr_err != LDAP_SUCCESS ) {
326 send_ldap_result( op, rs );
330 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
331 0, 0, &rs->sr_text, textbuf, textlen );
332 if ( rs->sr_err != LDAP_SUCCESS ) {
333 send_ldap_result( op, rs );
338 #ifdef SLAPD_MULTIMASTER
342 cb.sc_next = op->o_callback;
343 op->o_callback = &cb;
345 rc = op->o_bd->be_add( op, rs );
346 if ( rc == LDAP_SUCCESS ) {
347 /* NOTE: be_entry_release_w() is
348 * called by do_add(), so that global
349 * overlays on the way back can
350 * at least read the entry */
351 op->o_private = op->o_bd;
354 #ifndef SLAPD_MULTIMASTER
356 BerVarray defref = NULL;
358 defref = op->o_bd->be_update_refs
359 ? op->o_bd->be_update_refs : default_referral;
361 if ( defref != NULL ) {
362 rs->sr_ref = referral_rewrite( defref,
363 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
364 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
365 rs->sr_err = LDAP_REFERRAL;
366 if (!rs->sr_ref) rs->sr_ref = default_referral;
367 send_ldap_result( op, rs );
369 if ( rs->sr_ref != default_referral ) {
370 ber_bvarray_free( rs->sr_ref );
373 send_ldap_error( op, rs,
374 LDAP_UNWILLING_TO_PERFORM,
375 "shadow context; no update referral" );
377 #endif /* SLAPD_MULTIMASTER */
380 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
381 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
382 "operation not supported within namingContext" );
396 char *textbuf, size_t textlen )
401 assert( (*e)->e_attrs == NULL );
404 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
409 for( ; mods != NULL; mods = mods->sml_next ) {
412 assert( mods->sml_desc != NULL );
414 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
417 #define SLURPD_FRIENDLY
418 #ifdef SLURPD_FRIENDLY
421 for( i=0; attr->a_vals[i].bv_val; i++ ) {
424 for( j=0; mods->sml_values[j].bv_val; j++ ) {
429 attr->a_vals = ch_realloc( attr->a_vals,
430 sizeof( struct berval ) * (i+j) );
432 /* should check for duplicates */
435 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
436 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
438 BER_BVZERO( &attr->a_vals[i+j] );
440 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
441 sizeof( struct berval ) * j );
442 ch_free( mods->sml_values );
443 mods->sml_values = NULL;
446 if( mods->sml_nvalues ) {
447 attr->a_nvals = ch_realloc( attr->a_nvals,
448 sizeof( struct berval ) * (i+j) );
450 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
451 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
453 BER_BVZERO( &attr->a_nvals[i+j] );
455 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
456 sizeof( struct berval ) * j );
457 ch_free( mods->sml_nvalues );
458 mods->sml_nvalues = NULL;
461 attr->a_nvals = attr->a_vals;
466 snprintf( textbuf, textlen,
467 "attribute '%s' provided more than once",
468 mods->sml_desc->ad_cname.bv_val );
469 return LDAP_TYPE_OR_VALUE_EXISTS;
473 if( mods->sml_values[1].bv_val != NULL ) {
474 /* check for duplicates */
476 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
478 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
479 /* test asserted values against themselves */
480 for( j = 0; j < i; j++ ) {
481 rc = ordered_value_match( &match, mods->sml_desc, mr,
483 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
484 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
485 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
487 ? &mods->sml_nvalues[i]
488 : &mods->sml_values[i],
490 ? &mods->sml_nvalues[j]
491 : &mods->sml_values[j],
494 if ( rc == LDAP_SUCCESS && match == 0 ) {
495 /* value exists already */
496 snprintf( textbuf, textlen,
497 "%s: value #%d provided more than once",
498 mods->sml_desc->ad_cname.bv_val, j );
499 return LDAP_TYPE_OR_VALUE_EXISTS;
501 } else if ( rc != LDAP_SUCCESS ) {
508 attr = ch_calloc( 1, sizeof(Attribute) );
510 /* move ad to attr structure */
511 attr->a_desc = mods->sml_desc;
512 if ( !dup ) mods->sml_desc = NULL;
514 /* move values to attr structure */
515 /* should check for duplicates */
518 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
519 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
520 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
521 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
523 BER_BVZERO( &attr->a_vals[i] );
525 attr->a_vals = mods->sml_values;
526 mods->sml_values = NULL;
529 if ( mods->sml_nvalues ) {
532 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
533 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
534 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
535 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
537 BER_BVZERO( &attr->a_nvals[i] );
539 attr->a_nvals = mods->sml_nvalues;
540 mods->sml_nvalues = NULL;
543 attr->a_nvals = attr->a_vals;
547 tail = &attr->a_next;
558 Modifications **mods,
560 char *textbuf, size_t textlen )
562 Modifications *modhead = NULL;
564 Modifications **modtail = &modhead;
566 AttributeDescription *a_new_desc;
571 while ( a_new != NULL ) {
572 a_new_desc = a_new->a_desc;
573 mod = (Modifications *) malloc( sizeof( Modifications ));
575 mod->sml_op = LDAP_MOD_REPLACE;
578 mod->sml_type = a_new_desc->ad_cname;
580 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
582 mod->sml_values = (struct berval*) malloc(
583 (count+1) * sizeof( struct berval) );
585 /* see slap_mods_check() comments...
586 * if a_vals == a_nvals, there is no normalizer.
587 * in this case, mod->sml_nvalues must be left NULL.
589 if ( a_new->a_vals != a_new->a_nvals ) {
590 mod->sml_nvalues = (struct berval*) malloc(
591 (count+1) * sizeof( struct berval) );
593 mod->sml_nvalues = NULL;
596 for ( i = 0; i < count; i++ ) {
597 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
598 if ( mod->sml_nvalues ) {
599 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
603 mod->sml_values[count].bv_val = NULL;
604 mod->sml_values[count].bv_len = 0;
606 if ( mod->sml_nvalues ) {
607 mod->sml_nvalues[count].bv_val = NULL;
608 mod->sml_nvalues[count].bv_len = 0;
611 mod->sml_desc = a_new_desc;
614 modtail = &mod->sml_next;
615 a_new = a_new->a_next;