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 *modlist = op->ora_modlist;
218 Modifications **modtail = &modlist;
220 BackendDB *op_be, *bd = op->o_bd;
221 char textbuf[ SLAP_TEXT_BUFLEN ];
222 size_t textlen = sizeof( textbuf );
224 manageDSAit = get_manageDSAit( op );
227 * We could be serving multiple database backends. Select the
228 * appropriate one, or send a referral to our "referral server"
229 * if we don't hold it.
231 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 1 );
232 if ( op->o_bd == NULL ) {
234 rs->sr_ref = referral_rewrite( default_referral,
235 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
236 if ( !rs->sr_ref ) rs->sr_ref = default_referral;
238 rs->sr_err = LDAP_REFERRAL;
239 send_ldap_result( op, rs );
241 if ( rs->sr_ref != default_referral ) {
242 ber_bvarray_free( rs->sr_ref );
245 op->o_bd = frontendDB;
246 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
247 "no global superior knowledge" );
253 /* If we've got a glued backend, check the real backend */
255 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
256 op->o_bd = select_backend( &op->ora_e->e_nname, manageDSAit, 0 );
259 /* check restrictions */
260 if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
261 send_ldap_result( op, rs );
265 /* check for referrals */
266 if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
270 rs->sr_err = slap_mods_obsolete_check( op, modlist,
271 &rs->sr_text, textbuf, textlen );
273 if ( rs->sr_err != LDAP_SUCCESS ) {
274 send_ldap_result( op, rs );
279 * do the add if 1 && (2 || 3)
280 * 1) there is an add function implemented in this backend;
281 * 2) this backend is master for what it holds;
282 * 3) it's a replica and the dn supplied is the updatedn.
284 if ( op->o_bd->be_add ) {
285 /* do the update here */
286 int repl_user = be_isupdate( op );
287 #ifndef SLAPD_MULTIMASTER
288 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
291 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
292 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
297 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
298 &rs->sr_text, textbuf, textlen );
300 if ( rs->sr_err != LDAP_SUCCESS ) {
301 send_ldap_result( op, rs );
307 /* go to the last mod */
308 for ( modtail = &modlist;
310 modtail = &(*modtail)->sml_next )
312 assert( (*modtail)->sml_op == LDAP_MOD_ADD );
313 assert( (*modtail)->sml_desc != NULL );
317 /* check for duplicate values */
318 rs->sr_err = slap_mods_no_repl_user_mod_check( op,
319 modlist, &rs->sr_text, textbuf, textlen );
320 if ( rs->sr_err != LDAP_SUCCESS ) {
321 send_ldap_result( op, rs );
325 rs->sr_err = slap_mods2entry( *modtail, &op->ora_e,
326 0, 0, &rs->sr_text, textbuf, textlen );
327 if ( rs->sr_err != LDAP_SUCCESS ) {
328 send_ldap_result( op, rs );
333 #ifdef SLAPD_MULTIMASTER
337 cb.sc_next = op->o_callback;
338 op->o_callback = &cb;
340 rc = op->o_bd->be_add( op, rs );
341 if ( rc == LDAP_SUCCESS ) {
342 /* NOTE: be_entry_release_w() is
343 * called by do_add(), so that global
344 * overlays on the way back can
345 * at least read the entry */
346 op->o_private = op->o_bd;
349 #ifndef SLAPD_MULTIMASTER
351 BerVarray defref = NULL;
353 defref = op->o_bd->be_update_refs
354 ? op->o_bd->be_update_refs : default_referral;
356 if ( defref != NULL ) {
357 rs->sr_ref = referral_rewrite( defref,
358 NULL, &op->ora_e->e_name, LDAP_SCOPE_DEFAULT );
359 if ( rs->sr_ref == NULL ) rs->sr_ref = defref;
360 rs->sr_err = LDAP_REFERRAL;
361 if (!rs->sr_ref) rs->sr_ref = default_referral;
362 send_ldap_result( op, rs );
364 if ( rs->sr_ref != default_referral ) {
365 ber_bvarray_free( rs->sr_ref );
368 send_ldap_error( op, rs,
369 LDAP_UNWILLING_TO_PERFORM,
370 "shadow context; no update referral" );
372 #endif /* SLAPD_MULTIMASTER */
375 Debug( LDAP_DEBUG_ARGS, " do_add: no backend support\n", 0, 0, 0 );
376 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
377 "operation not supported within namingContext" );
392 char *textbuf, size_t textlen )
397 assert( (*e)->e_attrs == NULL );
400 for ( tail = &(*e)->e_attrs; *tail != NULL; tail = &(*tail)->a_next )
405 for( ; mods != NULL; mods = mods->sml_next ) {
408 assert( mods->sml_desc != NULL );
410 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
413 #define SLURPD_FRIENDLY
414 #ifdef SLURPD_FRIENDLY
419 * This check allows overlays to override operational
420 * attributes by setting them directly in the entry.
421 * We assume slap_mods_no_user_mod_check() was called
422 * with the user modifications.
428 for( i=0; attr->a_vals[i].bv_val; i++ ) {
431 for( j=0; mods->sml_values[j].bv_val; j++ ) {
436 attr->a_vals = ch_realloc( attr->a_vals,
437 sizeof( struct berval ) * (i+j) );
439 /* should check for duplicates */
442 for ( j = 0; mods->sml_values[j].bv_val; j++ ) {
443 ber_dupbv( &attr->a_vals[i+j], &mods->sml_values[j] );
445 BER_BVZERO( &attr->a_vals[i+j] );
448 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
449 sizeof( struct berval ) * j );
452 if( mods->sml_nvalues ) {
453 attr->a_nvals = ch_realloc( attr->a_nvals,
454 sizeof( struct berval ) * (i+j) );
456 for ( j = 0; mods->sml_nvalues[j].bv_val; j++ ) {
457 ber_dupbv( &attr->a_nvals[i+j], &mods->sml_nvalues[j] );
459 BER_BVZERO( &attr->a_nvals[i+j] );
461 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
462 sizeof( struct berval ) * j );
465 attr->a_nvals = attr->a_vals;
470 snprintf( textbuf, textlen,
471 "attribute '%s' provided more than once",
472 mods->sml_desc->ad_cname.bv_val );
474 return LDAP_TYPE_OR_VALUE_EXISTS;
478 if( mods->sml_values[1].bv_val != NULL ) {
479 /* check for duplicates */
481 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
483 for ( i = 1; mods->sml_values[i].bv_val != NULL; i++ ) {
484 /* test asserted values against themselves */
485 for( j = 0; j < i; j++ ) {
486 rc = ordered_value_match( &match, mods->sml_desc, mr,
488 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
489 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
490 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
492 ? &mods->sml_nvalues[i]
493 : &mods->sml_values[i],
495 ? &mods->sml_nvalues[j]
496 : &mods->sml_values[j],
499 if ( rc == LDAP_SUCCESS && match == 0 ) {
500 /* value exists already */
501 snprintf( textbuf, textlen,
502 "%s: value #%d provided more than once",
503 mods->sml_desc->ad_cname.bv_val, j );
505 return LDAP_TYPE_OR_VALUE_EXISTS;
507 } else if ( rc != LDAP_SUCCESS ) {
514 attr = ch_calloc( 1, sizeof(Attribute) );
516 /* move ad to attr structure */
517 attr->a_desc = mods->sml_desc;
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;
533 if ( mods->sml_nvalues ) {
536 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) /* EMPTY */;
537 attr->a_nvals = (BerVarray) ch_calloc( i+1, sizeof( BerValue ));
538 for ( i = 0; mods->sml_nvalues[i].bv_val; i++ ) {
539 ber_dupbv( &attr->a_nvals[i], &mods->sml_nvalues[i] );
541 BER_BVZERO( &attr->a_nvals[i] );
543 attr->a_nvals = mods->sml_nvalues;
546 attr->a_nvals = attr->a_vals;
550 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;
581 mod->sml_type = a_new_desc->ad_cname;
583 for ( count = 0; a_new->a_vals[count].bv_val; count++ ) /* EMPTY */;
585 mod->sml_values = (struct berval*) malloc(
586 (count+1) * sizeof( struct berval) );
588 /* see slap_mods_check() comments...
589 * if a_vals == a_nvals, there is no normalizer.
590 * in this case, mod->sml_nvalues must be left NULL.
592 if ( a_new->a_vals != a_new->a_nvals ) {
593 mod->sml_nvalues = (struct berval*) malloc(
594 (count+1) * sizeof( struct berval) );
596 mod->sml_nvalues = NULL;
599 for ( i = 0; i < count; i++ ) {
600 ber_dupbv(mod->sml_values+i, a_new->a_vals+i);
601 if ( mod->sml_nvalues ) {
602 ber_dupbv( mod->sml_nvalues+i, a_new->a_nvals+i );
606 mod->sml_values[count].bv_val = NULL;
607 mod->sml_values[count].bv_len = 0;
609 if ( mod->sml_nvalues ) {
610 mod->sml_nvalues[count].bv_val = NULL;
611 mod->sml_nvalues[count].bv_len = 0;
614 mod->sml_desc = a_new_desc;
617 modtail = &mod->sml_next;
618 a_new = a_new->a_next;
626 int slap_add_opattrs(
633 struct berval name, timestamp, csn = BER_BVNULL;
634 struct berval nname, tmp;
635 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
636 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
639 a = attr_find( op->ora_e->e_attrs,
640 slap_schema.si_ad_structuralObjectClass );
646 oc = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_objectClass );
648 rc = structural_class( oc->a_vals, &tmp, NULL, text,
650 if( rc != LDAP_SUCCESS ) return rc;
652 attr_merge_one( op->ora_e, slap_schema.si_ad_structuralObjectClass,
657 if ( SLAP_LASTMOD( op->o_bd ) ) {
660 timestamp.bv_val = timebuf;
662 a = attr_find( op->ora_e->e_attrs, slap_schema.si_ad_entryCSN );
667 if ( BER_BVISEMPTY( &op->o_csn )) {
670 csn.bv_len = sizeof(csnbuf);
671 slap_get_csn( op, &csn, manage_ctxcsn );
674 slap_queue_csn( op, &csn );
679 ptr = ber_bvchr( &csn, '#' );
681 timestamp.bv_len = ptr - csn.bv_val;
682 if ( timestamp.bv_len >= sizeof(timebuf) )
683 timestamp.bv_len = sizeof(timebuf) - 1;
684 strncpy( timebuf, csn.bv_val, timestamp.bv_len );
685 timebuf[timestamp.bv_len] = '\0';
687 time_t now = slap_get_time();
689 timestamp.bv_len = sizeof(timebuf);
691 slap_timestamp( &now, ×tamp );
694 if ( BER_BVISEMPTY( &op->o_dn ) ) {
695 BER_BVSTR( &name, SLAPD_ANONYMOUS );
702 a = attr_find( op->ora_e->e_attrs,
703 slap_schema.si_ad_entryUUID );
705 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
707 tmp.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
708 tmp.bv_val = uuidbuf;
710 attr_merge_normalize_one( op->ora_e,
711 slap_schema.si_ad_entryUUID, &tmp, op->o_tmpmemctx );
714 a = attr_find( op->ora_e->e_attrs,
715 slap_schema.si_ad_creatorsName );
717 attr_merge_one( op->ora_e,
718 slap_schema.si_ad_creatorsName, &name, &nname );
721 a = attr_find( op->ora_e->e_attrs,
722 slap_schema.si_ad_createTimestamp );
724 attr_merge_one( op->ora_e,
725 slap_schema.si_ad_createTimestamp, ×tamp, NULL );
729 attr_merge_one( op->ora_e,
730 slap_schema.si_ad_entryCSN, &csn, NULL );
733 a = attr_find( op->ora_e->e_attrs,
734 slap_schema.si_ad_modifiersName );
736 attr_merge_one( op->ora_e,
737 slap_schema.si_ad_modifiersName, &name, &nname );
740 a = attr_find( op->ora_e->e_attrs,
741 slap_schema.si_ad_modifyTimestamp );
743 attr_merge_one( op->ora_e,
744 slap_schema.si_ad_modifyTimestamp, ×tamp, NULL );