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_managing = 0;
114 mod->sml_op = LDAP_MOD_ADD;
116 mod->sml_next = NULL;
117 mod->sml_desc = NULL;
118 mod->sml_type = tmp.sml_type;
119 mod->sml_values = tmp.sml_values;
120 mod->sml_nvalues = NULL;
123 modtail = &mod->sml_next;
126 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
127 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
128 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
129 rs->sr_err = SLAPD_DISCONNECT;
133 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
134 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
138 if ( modlist == NULL ) {
139 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
140 "no attributes provided" );
144 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
145 op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
147 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
148 /* protocolError may be a more appropriate error */
149 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
150 "root DSE already exists" );
153 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
154 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
155 "subschema subentry already exists" );
159 rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
160 textbuf, textlen, NULL );
162 if ( rs->sr_err != LDAP_SUCCESS ) {
163 send_ldap_result( op, rs );
167 /* temporary; remove if not invoking backend function */
168 op->ora_modlist = modlist;
170 /* call this so global overlays/SLAPI have access to ora_e */
171 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
172 1, 0, &rs->sr_text, textbuf, textlen );
173 if ( rs->sr_err != LDAP_SUCCESS ) {
174 send_ldap_result( op, rs );
178 op->o_bd = frontendDB;
179 rc = frontendDB->be_add( op, rs );
181 if ( op->ora_e != NULL && op->o_private != NULL ) {
182 BackendDB *bd = op->o_bd;
184 op->o_bd = (BackendDB *)op->o_private;
185 op->o_private = NULL;
187 be_entry_release_w( op, op->ora_e );
191 op->o_private = NULL;
196 slap_graduate_commit_csn( op );
198 if ( modlist != NULL ) {
199 slap_mods_free( modlist, 0 );
202 if ( op->ora_e != NULL ) {
203 entry_free( op->ora_e );
205 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
206 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
212 fe_op_add( Operation *op, SlapReply *rs )
215 Modifications *modlist = op->ora_modlist;
216 Modifications **modtail = &modlist;
219 char textbuf[ SLAP_TEXT_BUFLEN ];
220 size_t textlen = sizeof( textbuf );
222 manageDSAit = get_manageDSAit( op );
225 * We could be serving multiple database backends. Select the
226 * appropriate one, or send a referral to our "referral server"
227 * if we don't hold it.
229 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
230 if ( op->o_bd == NULL ) {
231 rs->sr_ref = referral_rewrite( default_referral,
232 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
233 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
235 rs->sr_err = LDAP_REFERRAL;
236 op->o_bd = frontendDB;
237 send_ldap_result( op, rs );
240 if ( rs->sr_ref != default_referral ) {
241 ber_bvarray_free( rs->sr_ref );
244 op->o_bd = frontendDB;
245 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
246 "no global superior knowledge" );
252 /* If we've got a glued backend, check the real backend */
254 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
255 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
258 /* check restrictions */
259 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
260 send_ldap_result( op, rs );
264 /* check for referrals */
265 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
269 rs->sr_err = slap_mods_obsolete_check( op, modlist,
270 &rs->sr_text, textbuf, textlen );
272 if ( rs->sr_err != LDAP_SUCCESS ) {
273 send_ldap_result( op, rs );
278 * do the add if 1 && (2 || 3)
279 * 1) there is an add function implemented in this backend;
280 * 2) this backend is master for what it holds;
281 * 3) it's a replica and the dn supplied is the updatedn.
283 if ( op->o_bd->be_add ) {
284 /* do the update here */
285 int repl_user = be_isupdate( op );
286 #ifndef SLAPD_MULTIMASTER
287 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
290 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
291 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
296 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
297 &rs->sr_text, textbuf, textlen );
299 if ( rs->sr_err != LDAP_SUCCESS ) {
300 send_ldap_result( op, rs );
306 /* go to the last mod */
307 for ( modtail = &modlist;
309 modtail = &(*modtail)->sml_next )
311 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
312 assert( (*modtail)->sml_desc != NULL );
316 rs->sr_err = slap_mods_opattrs( op, modlist,
317 modtail, &rs->sr_text,
318 textbuf, textlen, 1 );
319 if ( rs->sr_err != LDAP_SUCCESS ) {
320 send_ldap_result( op, rs );
324 /* check for duplicate values */
325 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
326 modlist, &rs->sr_text, textbuf, textlen );
327 if ( rs->sr_err != LDAP_SUCCESS ) {
328 send_ldap_result( op, rs );
332 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
333 0, 0, &rs->sr_text, textbuf, textlen );
334 if ( rs->sr_err != LDAP_SUCCESS ) {
335 send_ldap_result( op, rs );
340 #ifdef SLAPD_MULTIMASTER
344 cb.sc_next = op->o_callback;
345 op->o_callback = &cb;
347 rc = op->o_bd->be_add( op, rs );
348 if ( rc == LDAP_SUCCESS ) {
349 /* NOTE: be_entry_release_w() is
350 * called by do_add(), so that global
351 * overlays on the way back can
352 * at least read the entry */
353 op->o_private = op->o_bd;
356 #ifndef SLAPD_MULTIMASTER
358 BerVarray defref = NULL;
360 defref = op->o_bd->be_update_refs
361 ? op->o_bd->be_update_refs : default_referral;
363 if ( defref != NULL ) {
364 rs->sr_ref = referral_rewrite( defref,
365 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
366 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
367 rs->sr_err = LDAP_REFERRAL;
368 if (!rs->sr_ref) rs->sr_ref = default_referral;
369 send_ldap_result( op, rs );
371 if ( rs->sr_ref != default_referral ) {
372 ber_bvarray_free( rs->sr_ref );
375 send_ldap_error( op, rs,
376 LDAP_UNWILLING_TO_PERFORM,
377 "shadow context; no update referral" );
379 #endif /* SLAPD_MULTIMASTER */
382 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
383 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
384 "operation not supported within namingContext" );
398 char *textbuf, size_t textlen )
403 assert( (*e)->e_attrs == NULL );
406 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
411 for( ; mods != NULL; mods = mods->sml_next ) {
414 assert( mods->sml_desc != NULL );
416 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
419 #define SLURPD_FRIENDLY
420 #ifdef SLURPD_FRIENDLY
425 * This check allows overlays to override operational
426 * attributes by setting them directly in the entry.
427 * We assume slap_mods_no_user_mod_check() was called
428 * with the user modifications.
434 for( i=0; attr->a_vals[i].bv_val; i++ ) {
437 for( j=0; mods->sml_values[j].bv_val; j++ ) {
442 attr->a_vals = ch_realloc( attr->a_vals,
443 sizeof( struct berval ) * (i+j) );
445 /* should check for duplicates */
448 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
449 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
451 BER_BVZERO( &attr->a_vals[i+j] );
454 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
455 sizeof( struct berval ) * j );
458 if( mods->sml_nvalues ) {
459 attr->a_nvals = ch_realloc( attr->a_nvals,
460 sizeof( struct berval ) * (i+j) );
462 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
463 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
465 BER_BVZERO( &attr->a_nvals[i+j] );
467 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
468 sizeof( struct berval ) * j );
471 attr->a_nvals = attr->a_vals;
476 snprintf( textbuf, textlen,
477 "attribute '%s' provided more than once",
478 mods->sml_desc->ad_cname.bv_val );
479 return LDAP_TYPE_OR_VALUE_EXISTS;
483 if( mods->sml_values[1].bv_val != NULL ) {
484 /* check for duplicates */
486 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
488 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
489 /* test asserted values against themselves */
490 for( j = 0; j < i; j++ ) {
491 rc = ordered_value_match( &match, mods->sml_desc, mr,
493 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
494 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
495 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
497 ? &mods->sml_nvalues[i]
498 : &mods->sml_values[i],
500 ? &mods->sml_nvalues[j]
501 : &mods->sml_values[j],
504 if ( rc == LDAP_SUCCESS && match == 0 ) {
505 /* value exists already */
506 snprintf( textbuf, textlen,
507 "%s: value #%d provided more than once",
508 mods->sml_desc->ad_cname.bv_val, j );
509 return LDAP_TYPE_OR_VALUE_EXISTS;
511 } else if ( rc != LDAP_SUCCESS ) {
518 attr = ch_calloc( 1, sizeof(Attribute) );
520 /* move ad to attr structure */
521 attr->a_desc = mods->sml_desc;
523 /* move values to attr structure */
524 /* should check for duplicates */
527 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
528 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
529 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
530 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
532 BER_BVZERO( &attr->a_vals[i] );
534 attr->a_vals = mods->sml_values;
537 if ( mods->sml_nvalues ) {
540 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
541 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
542 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
543 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
545 BER_BVZERO( &attr->a_nvals[i] );
547 attr->a_nvals = mods->sml_nvalues;
550 attr->a_nvals = attr->a_vals;
554 tail = &attr->a_next;
565 Modifications **mods,
567 char *textbuf, size_t textlen )
569 Modifications *modhead = NULL;
571 Modifications **modtail = &modhead;
573 AttributeDescription *a_new_desc;
578 while ( a_new != NULL ) {
579 a_new_desc = a_new->a_desc;
580 mod = (Modifications *) malloc( sizeof( Modifications ));
581 mod->sml_managing = 0;
583 mod->sml_op = LDAP_MOD_REPLACE;
586 mod->sml_type = a_new_desc->ad_cname;
588 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
590 mod->sml_values = (struct berval*) malloc(
591 (count+1) * sizeof( struct berval) );
593 /* see slap_mods_check() comments...
594 * if a_vals == a_nvals, there is no normalizer.
595 * in this case, mod->sml_nvalues must be left NULL.
597 if ( a_new->a_vals != a_new->a_nvals ) {
598 mod->sml_nvalues = (struct berval*) malloc(
599 (count+1) * sizeof( struct berval) );
601 mod->sml_nvalues = NULL;
604 for ( i = 0; i < count; i++ ) {
605 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
606 if ( mod->sml_nvalues ) {
607 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
611 mod->sml_values[count].bv_val = NULL;
612 mod->sml_values[count].bv_len = 0;
614 if ( mod->sml_nvalues ) {
615 mod->sml_nvalues[count].bv_val = NULL;
616 mod->sml_nvalues[count].bv_len = 0;
619 mod->sml_desc = a_new_desc;
622 modtail = &mod->sml_next;
623 a_new = a_new->a_next;