2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2006 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 do_add( Operation *op, SlapReply *rs )
39 BerElement *ber = op->o_ber;
41 struct berval dn = BER_BVNULL;
44 Modifications *modlist = NULL;
45 Modifications **modtail = &modlist;
47 char textbuf[ SLAP_TEXT_BUFLEN ];
48 size_t textlen = sizeof( textbuf );
52 Debug( LDAP_DEBUG_TRACE, "do_add\n", 0, 0, 0 );
54 * Parse the add request. It looks like this:
56 * AddRequest := [APPLICATION 14] SEQUENCE {
57 * name DistinguishedName,
58 * attrs SEQUENCE OF SEQUENCE {
60 * values SET OF AttributeValue
66 if ( ber_scanf( ber, "{m", /*}*/ &dn ) == LBER_ERROR ) {
67 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
68 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
69 return SLAPD_DISCONNECT;
72 op->ora_e = (Entry *) ch_calloc( 1, sizeof(Entry) );
74 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
77 if ( rs->sr_err != LDAP_SUCCESS ) {
78 Debug( LDAP_DEBUG_ANY, "do_add: invalid dn (%s)\n", dn.bv_val, 0, 0 );
79 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
83 ber_dupbv( &op->ora_e->e_name, &op->o_req_dn );
84 ber_dupbv( &op->ora_e->e_nname, &op->o_req_ndn );
86 Debug( LDAP_DEBUG_ARGS, "do_add: dn (%s)\n", op->ora_e->e_dn, 0, 0 );
89 for ( tag = ber_first_element( ber, &len, &last ); tag != LBER_DEFAULT;
90 tag = ber_next_element( ber, &len, last ) )
95 tmp.sml_nvalues = NULL;
97 rtag = ber_scanf( ber, "{m{W}}", &tmp.sml_type, &tmp.sml_values );
99 if ( rtag == LBER_ERROR ) {
100 Debug( LDAP_DEBUG_ANY, "do_add: decoding error\n", 0, 0, 0 );
101 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
102 rs->sr_err = SLAPD_DISCONNECT;
106 if ( tmp.sml_values == NULL ) {
107 Debug( LDAP_DEBUG_ANY, "no values for type %s\n",
108 tmp.sml_type.bv_val, 0, 0 );
109 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
110 "no values for attribute type" );
114 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
115 mod->sml_op = LDAP_MOD_ADD;
117 mod->sml_next = NULL;
118 mod->sml_desc = NULL;
119 mod->sml_type = tmp.sml_type;
120 mod->sml_values = tmp.sml_values;
121 mod->sml_nvalues = NULL;
124 modtail = &mod->sml_next;
127 if ( ber_scanf( ber, /*{*/ "}") == LBER_ERROR ) {
128 Debug( LDAP_DEBUG_ANY, "do_add: ber_scanf failed\n", 0, 0, 0 );
129 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
130 rs->sr_err = SLAPD_DISCONNECT;
134 if ( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
135 Debug( LDAP_DEBUG_ANY, "do_add: get_ctrls failed\n", 0, 0, 0 );
139 if ( modlist == NULL ) {
140 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
141 "no attributes provided" );
145 Statslog( LDAP_DEBUG_STATS, "%s ADD dn=\"%s\"\n",
146 op->o_log_prefix, op->ora_e->e_name.bv_val, 0, 0, 0 );
148 if ( dn_match( &op->ora_e->e_nname, &slap_empty_bv ) ) {
149 /* protocolError may be a more appropriate error */
150 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
151 "root DSE already exists" );
154 } else if ( dn_match( &op->ora_e->e_nname, &frontendDB->be_schemandn ) ) {
155 send_ldap_error( op, rs, LDAP_ALREADY_EXISTS,
156 "subschema subentry already exists" );
160 rs->sr_err = slap_mods_check( modlist, &rs->sr_text,
161 textbuf, textlen, NULL );
163 if ( rs->sr_err != LDAP_SUCCESS ) {
164 send_ldap_result( op, rs );
168 /* temporary; remove if not invoking backend function */
169 op->ora_modlist = modlist;
171 /* call this so global overlays/SLAPI have access to ora_e */
172 rs->sr_err = slap_mods2entry( op->ora_modlist, &op->ora_e,
173 1, 0, &rs->sr_text, textbuf, textlen );
174 if ( rs->sr_err != LDAP_SUCCESS ) {
175 send_ldap_result( op, rs );
181 op->o_bd = frontendDB;
182 rc = frontendDB->be_add( op, rs );
184 if ( op->ora_e != NULL && op->o_private != NULL ) {
185 BackendDB *bd = op->o_bd;
187 op->o_bd = (BackendDB *)op->o_private;
188 op->o_private = NULL;
190 be_entry_release_w( op, op->ora_e );
194 op->o_private = NULL;
199 if ( modlist != NULL ) {
200 /* in case of error, free the values as well */
201 slap_mods_free( modlist, freevals );
204 if ( op->ora_e != NULL ) {
205 entry_free( op->ora_e );
207 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
208 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
214 fe_op_add( Operation *op, SlapReply *rs )
217 Modifications **modtail = &op->ora_modlist;
219 BackendDB *op_be, *bd = op->o_bd;
220 char textbuf[ SLAP_TEXT_BUFLEN ];
221 size_t textlen = sizeof( textbuf );
223 manageDSAit = get_manageDSAit( op );
226 * We could be serving multiple database backends. Select the
227 * appropriate one, or send a referral to our "referral server"
228 * if we don't hold it.
230 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
231 if ( op->o_bd == NULL ) {
233 rs->sr_ref = referral_rewrite( default_referral,
234 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
235 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
237 rs->sr_err = LDAP_REFERRAL;
238 send_ldap_result( op, rs );
240 if ( rs->sr_ref != default_referral ) {
241 ber_bvarray_free( rs->sr_ref );
244 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
245 "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, op->ora_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 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user )
286 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
287 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
292 rs->sr_err = slap_mods_no_user_mod_check( op, op->ora_modlist,
293 &rs->sr_text, textbuf, textlen );
295 if ( rs->sr_err != LDAP_SUCCESS ) {
296 send_ldap_result( op, rs );
302 /* go to the last mod */
303 for ( modtail = &op->ora_modlist;
305 modtail = &(*modtail)->sml_next )
307 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
308 assert( (*modtail)->sml_desc != NULL );
312 /* check for duplicate values */
313 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
314 op->ora_modlist, &rs->sr_text, textbuf, textlen );
315 if ( rs->sr_err != LDAP_SUCCESS ) {
316 send_ldap_result( op, rs );
320 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
321 0, 0, &rs->sr_text, textbuf, textlen );
322 if ( rs->sr_err != LDAP_SUCCESS ) {
323 send_ldap_result( op, rs );
327 cb.sc_next = op->o_callback;
328 op->o_callback = &cb;
331 rc = op->o_bd->be_add( op, rs );
332 if ( rc == LDAP_SUCCESS ) {
333 /* NOTE: be_entry_release_w() is
334 * called by do_add(), so that global
335 * overlays on the way back can
336 * at least read the entry */
337 op->o_private = op->o_bd;
341 BerVarray defref = NULL;
343 defref = op->o_bd->be_update_refs
344 ? op->o_bd->be_update_refs : default_referral;
346 if ( defref != NULL ) {
347 rs->sr_ref = referral_rewrite( defref,
348 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
349 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
350 rs->sr_err = LDAP_REFERRAL;
351 if (!rs->sr_ref) rs->sr_ref = default_referral;
352 send_ldap_result( op, rs );
354 if ( rs->sr_ref != default_referral ) {
355 ber_bvarray_free( rs->sr_ref );
358 send_ldap_error( op, rs,
359 LDAP_UNWILLING_TO_PERFORM,
360 "shadow context; no update referral" );
364 Debug( LDAP_DEBUG_ARGS, "do_add: no backend support\n", 0, 0, 0 );
365 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
366 "operation not supported within namingContext" );
381 char *textbuf, size_t textlen )
386 assert( (*e)->e_attrs == NULL );
389 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
394 for( ; mods != NULL; mods = mods->sml_next ) {
397 assert( mods->sml_desc != NULL );
399 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
402 #define SLURPD_FRIENDLY
403 #ifdef SLURPD_FRIENDLY
408 * This check allows overlays to override operational
409 * attributes by setting them directly in the entry.
410 * We assume slap_mods_no_user_mod_check() was called
411 * with the user modifications.
417 for( i=0; attr->a_vals[i].bv_val; i++ ) {
420 for( j=0; mods->sml_values[j].bv_val; j++ ) {
425 attr->a_vals = ch_realloc( attr->a_vals,
426 sizeof( struct berval ) * (i+j) );
428 /* should check for duplicates */
431 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
432 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
434 BER_BVZERO( &attr->a_vals[i+j] );
437 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
438 sizeof( struct berval ) * j );
441 if( mods->sml_nvalues ) {
442 attr->a_nvals = ch_realloc( attr->a_nvals,
443 sizeof( struct berval ) * (i+j) );
445 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
446 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
448 BER_BVZERO( &attr->a_nvals[i+j] );
450 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
451 sizeof( struct berval ) * j );
454 attr->a_nvals = attr->a_vals;
459 snprintf( textbuf, textlen,
460 "attribute '%s' provided more than once",
461 mods->sml_desc->ad_cname.bv_val );
463 return LDAP_TYPE_OR_VALUE_EXISTS;
467 if( mods->sml_values[1].bv_val != NULL ) {
468 /* check for duplicates */
470 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
472 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
473 /* test asserted values against themselves */
474 for( j = 0; j < i; j++ ) {
475 rc = ordered_value_match( &match, mods->sml_desc, mr,
477 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
478 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
479 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
481 ? &mods->sml_nvalues[i]
482 : &mods->sml_values[i],
484 ? &mods->sml_nvalues[j]
485 : &mods->sml_values[j],
488 if ( rc == LDAP_SUCCESS && match == 0 ) {
489 /* value exists already */
490 snprintf( textbuf, textlen,
491 "%s: value #%d provided more than once",
492 mods->sml_desc->ad_cname.bv_val, j );
494 return LDAP_TYPE_OR_VALUE_EXISTS;
496 } else if ( rc != LDAP_SUCCESS ) {
503 attr = ch_calloc( 1, sizeof(Attribute) );
505 /* move ad to attr structure */
506 attr->a_desc = mods->sml_desc;
508 /* move values to attr structure */
509 /* should check for duplicates */
512 for ( i = 0; mods->sml_values[i].bv_val; i++ ) /* EMPTY */;
513 attr->a_vals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
514 for ( i = 0; mods->sml_values[i].bv_val; i++ ) {
515 ber_dupbv( &attr->a_vals[i], &mods->sml_values[i] );
517 BER_BVZERO( &attr->a_vals[i] );
519 attr->a_vals = mods->sml_values;
522 if ( mods->sml_nvalues ) {
525 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
526 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
527 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
528 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
530 BER_BVZERO( &attr->a_nvals[i] );
532 attr->a_nvals = mods->sml_nvalues;
535 attr->a_nvals = attr->a_vals;
539 tail = &attr->a_next;
550 Modifications **mods,
552 char *textbuf, size_t textlen )
554 Modifications *modhead = NULL;
556 Modifications **modtail = &modhead;
558 AttributeDescription *a_new_desc;
563 while ( a_new != NULL ) {
564 a_new_desc = a_new->a_desc;
565 mod = (Modifications *) malloc( sizeof( Modifications ));
567 mod->sml_op = LDAP_MOD_REPLACE;
570 mod->sml_type = a_new_desc->ad_cname;
572 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
574 mod->sml_values = (struct berval*) malloc(
575 (count+1) * sizeof( struct berval) );
577 /* see slap_mods_check() comments...
578 * if a_vals == a_nvals, there is no normalizer.
579 * in this case, mod->sml_nvalues must be left NULL.
581 if ( a_new->a_vals != a_new->a_nvals ) {
582 mod->sml_nvalues = (struct berval*) malloc(
583 (count+1) * sizeof( struct berval) );
585 mod->sml_nvalues = NULL;
588 for ( i = 0; i < count; i++ ) {
589 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
590 if ( mod->sml_nvalues ) {
591 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
595 mod->sml_values[count].bv_val = NULL;
596 mod->sml_values[count].bv_len = 0;
598 if ( mod->sml_nvalues ) {
599 mod->sml_nvalues[count].bv_val = NULL;
600 mod->sml_nvalues[count].bv_len = 0;
603 mod->sml_desc = a_new_desc;
606 modtail = &mod->sml_next;
607 a_new = a_new->a_next;
615 int slap_add_opattrs(
622 struct berval name, timestamp, csn = BER_BVNULL;
623 struct berval nname, tmp;
624 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
625 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
628 a = attr_find( op->ora_e->e_attrs,
629 slap_schema.si_ad_structuralObjectClass );
635 oc = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_objectClass );
637 rc = structural_class( oc->a_vals, &tmp, NULL, text,
639 if( rc != LDAP_SUCCESS ) return rc;
641 attr_merge_one( op->ora_e, slap_schema.si_ad_structuralObjectClass,
646 if ( SLAP_LASTMOD( op->o_bd ) ) {
650 timestamp.bv_val = timebuf;
651 a = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_entryCSN );
656 if ( BER_BVISEMPTY( &op->o_csn )) {
659 csn.bv_len = sizeof(csnbuf);
660 slap_get_csn( op, &csn, manage_ctxcsn );
663 slap_queue_csn( op, &csn );
668 ptr = ber_bvchr( &csn, '#' );
670 timestamp.bv_len = ptr - csn.bv_val;
671 if ( timestamp.bv_len >= sizeof(timebuf) ) /* ?!? */
672 timestamp.bv_len = sizeof(timebuf) - 1;
673 AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
674 timebuf[timestamp.bv_len] = '\0';
676 time_t now = slap_get_time();
678 timestamp.bv_len = sizeof(timebuf);
680 slap_timestamp( &now, ×tamp );
683 if ( BER_BVISEMPTY( &op->o_dn ) ) {
684 BER_BVSTR( &name, SLAPD_ANONYMOUS );
691 a = attr_find( op->ora_e->e_attrs,
692 slap_schema.si_ad_entryUUID );
694 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
696 tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
697 tmp.bv_val = uuidbuf;
699 attr_merge_normalize_one( op->ora_e,
700 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
703 a = attr_find( op->ora_e->e_attrs,
704 slap_schema.si_ad_creatorsName );
706 attr_merge_one( op->ora_e,
707 slap_schema.si_ad_creatorsName, &name, &nname );
710 a = attr_find( op->ora_e->e_attrs,
711 slap_schema.si_ad_createTimestamp );
713 attr_merge_one( op->ora_e,
714 slap_schema.si_ad_createTimestamp, ×tamp, NULL );
718 attr_merge_one( op->ora_e,
719 slap_schema.si_ad_entryCSN, &csn, NULL );
722 a = attr_find( op->ora_e->e_attrs,
723 slap_schema.si_ad_modifiersName );
725 attr_merge_one( op->ora_e,
726 slap_schema.si_ad_modifiersName, &name, &nname );
729 a = attr_find( op->ora_e->e_attrs,
730 slap_schema.si_ad_modifyTimestamp );
732 attr_merge_one( op->ora_e,
733 slap_schema.si_ad_modifyTimestamp, ×tamp, NULL );