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, *next;
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, 0 );
201 if ( op->ora_e != NULL ) {
202 entry_free( op->ora_e );
204 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
205 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
211 fe_op_add( Operation *op, SlapReply *rs )
214 Modifications *modlist = op->ora_modlist;
215 Modifications **modtail = &modlist;
218 char textbuf[ SLAP_TEXT_BUFLEN ];
219 size_t textlen = sizeof( textbuf );
221 manageDSAit = get_manageDSAit( op );
224 * We could be serving multiple database backends. Select the
225 * appropriate one, or send a referral to our "referral server"
226 * if we don't hold it.
228 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
229 if ( op->o_bd == NULL ) {
230 rs->sr_ref = referral_rewrite( default_referral,
231 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
232 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
234 rs->sr_err = LDAP_REFERRAL;
235 op->o_bd = frontendDB;
236 send_ldap_result( op, rs );
239 if ( rs->sr_ref != default_referral ) {
240 ber_bvarray_free( rs->sr_ref );
243 op->o_bd = frontendDB;
244 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
245 "no global superior knowledge" );
251 /* If we've got a glued backend, check the real backend */
253 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
254 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
257 /* check restrictions */
258 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
259 send_ldap_result( op, rs );
263 /* check for referrals */
264 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
268 rs->sr_err = slap_mods_obsolete_check( op, modlist,
269 &rs->sr_text, textbuf, textlen );
271 if ( rs->sr_err != LDAP_SUCCESS ) {
272 send_ldap_result( op, rs );
277 * do the add if 1 && (2 || 3)
278 * 1) there is an add function implemented in this backend;
279 * 2) this backend is master for what it holds;
280 * 3) it's a replica and the dn supplied is the updatedn.
282 if ( op->o_bd->be_add ) {
283 /* do the update here */
284 int repl_user = be_isupdate( op );
285 #ifndef SLAPD_MULTIMASTER
286 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
289 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
290 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
295 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
296 &rs->sr_text, textbuf, textlen );
298 if ( rs->sr_err != LDAP_SUCCESS ) {
299 send_ldap_result( op, rs );
305 /* go to the last mod */
306 for ( modtail = &modlist;
308 modtail = &(*modtail)->sml_next )
310 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
311 assert( (*modtail)->sml_desc != NULL );
315 rs->sr_err = slap_mods_opattrs( op, modlist,
316 modtail, &rs->sr_text,
317 textbuf, textlen, 1 );
318 if ( rs->sr_err != LDAP_SUCCESS ) {
319 send_ldap_result( op, rs );
323 /* check for duplicate values */
324 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
325 modlist, &rs->sr_text, textbuf, textlen );
326 if ( rs->sr_err != LDAP_SUCCESS ) {
327 send_ldap_result( op, rs );
331 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
332 0, 0, &rs->sr_text, textbuf, textlen );
333 if ( rs->sr_err != LDAP_SUCCESS ) {
334 send_ldap_result( op, rs );
339 #ifdef SLAPD_MULTIMASTER
343 cb.sc_next = op->o_callback;
344 op->o_callback = &cb;
346 rc = op->o_bd->be_add( op, rs );
347 if ( rc == LDAP_SUCCESS ) {
348 /* NOTE: be_entry_release_w() is
349 * called by do_add(), so that global
350 * overlays on the way back can
351 * at least read the entry */
352 op->o_private = op->o_bd;
355 #ifndef SLAPD_MULTIMASTER
357 BerVarray defref = NULL;
359 defref = op->o_bd->be_update_refs
360 ? op->o_bd->be_update_refs : default_referral;
362 if ( defref != NULL ) {
363 rs->sr_ref = referral_rewrite( defref,
364 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
365 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
366 rs->sr_err = LDAP_REFERRAL;
367 if (!rs->sr_ref) rs->sr_ref = default_referral;
368 send_ldap_result( op, rs );
370 if ( rs->sr_ref != default_referral ) {
371 ber_bvarray_free( rs->sr_ref );
374 send_ldap_error( op, rs,
375 LDAP_UNWILLING_TO_PERFORM,
376 "shadow context; no update referral" );
378 #endif /* SLAPD_MULTIMASTER */
381 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
382 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
383 "operation not supported within namingContext" );
397 char *textbuf, size_t textlen )
402 assert( (*e)->e_attrs == NULL );
405 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
410 for( ; mods != NULL; mods = mods->sml_next ) {
413 assert( mods->sml_desc != NULL );
415 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
418 #define SLURPD_FRIENDLY
419 #ifdef SLURPD_FRIENDLY
424 * This check allows overlays to override operational
425 * attributes by setting them directly in the entry.
426 * We assume slap_mods_no_user_mod_check() was called
427 * with the user modifications.
433 for( i=0; attr->a_vals[i].bv_val; i++ ) {
436 for( j=0; mods->sml_values[j].bv_val; j++ ) {
441 attr->a_vals = ch_realloc( attr->a_vals,
442 sizeof( struct berval ) * (i+j) );
444 /* should check for duplicates */
447 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
448 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
450 BER_BVZERO( &attr->a_vals[i+j] );
453 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
454 sizeof( struct berval ) * j );
457 if( mods->sml_nvalues ) {
458 attr->a_nvals = ch_realloc( attr->a_nvals,
459 sizeof( struct berval ) * (i+j) );
461 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
462 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
464 BER_BVZERO( &attr->a_nvals[i+j] );
466 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
467 sizeof( struct berval ) * j );
470 attr->a_nvals = attr->a_vals;
475 snprintf( textbuf, textlen,
476 "attribute '%s' provided more than once",
477 mods->sml_desc->ad_cname.bv_val );
478 return LDAP_TYPE_OR_VALUE_EXISTS;
482 if( mods->sml_values[1].bv_val != NULL ) {
483 /* check for duplicates */
485 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
487 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
488 /* test asserted values against themselves */
489 for( j = 0; j < i; j++ ) {
490 rc = ordered_value_match( &match, mods->sml_desc, mr,
492 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
493 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
494 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
496 ? &mods->sml_nvalues[i]
497 : &mods->sml_values[i],
499 ? &mods->sml_nvalues[j]
500 : &mods->sml_values[j],
503 if ( rc == LDAP_SUCCESS && match == 0 ) {
504 /* value exists already */
505 snprintf( textbuf, textlen,
506 "%s: value #%d provided more than once",
507 mods->sml_desc->ad_cname.bv_val, j );
508 return LDAP_TYPE_OR_VALUE_EXISTS;
510 } else if ( rc != LDAP_SUCCESS ) {
517 attr = ch_calloc( 1, sizeof(Attribute) );
519 /* move ad to attr structure */
520 attr->a_desc = mods->sml_desc;
522 /* move values to attr structure */
523 /* should check for duplicates */
526 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
527 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
528 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
529 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
531 BER_BVZERO( &attr->a_vals[i] );
533 attr->a_vals = mods->sml_values;
536 if ( mods->sml_nvalues ) {
539 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
540 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
541 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
542 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
544 BER_BVZERO( &attr->a_nvals[i] );
546 attr->a_nvals = mods->sml_nvalues;
549 attr->a_nvals = attr->a_vals;
553 tail = &attr->a_next;
564 Modifications **mods,
566 char *textbuf, size_t textlen )
568 Modifications *modhead = NULL;
570 Modifications **modtail = &modhead;
572 AttributeDescription *a_new_desc;
577 while ( a_new != NULL ) {
578 a_new_desc = a_new->a_desc;
579 mod = (Modifications *) malloc( sizeof( Modifications ));
581 mod->sml_op = LDAP_MOD_REPLACE;
584 mod->sml_type = a_new_desc->ad_cname;
586 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
588 mod->sml_values = (struct berval*) malloc(
589 (count+1) * sizeof( struct berval) );
591 /* see slap_mods_check() comments...
592 * if a_vals == a_nvals, there is no normalizer.
593 * in this case, mod->sml_nvalues must be left NULL.
595 if ( a_new->a_vals != a_new->a_nvals ) {
596 mod->sml_nvalues = (struct berval*) malloc(
597 (count+1) * sizeof( struct berval) );
599 mod->sml_nvalues = NULL;
602 for ( i = 0; i < count; i++ ) {
603 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
604 if ( mod->sml_nvalues ) {
605 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
609 mod->sml_values[count].bv_val = NULL;
610 mod->sml_values[count].bv_len = 0;
612 if ( mod->sml_nvalues ) {
613 mod->sml_nvalues[count].bv_val = NULL;
614 mod->sml_nvalues[count].bv_len = 0;
617 mod->sml_desc = a_new_desc;
620 modtail = &mod->sml_next;
621 a_new = a_new->a_next;