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.
30 #include <ac/socket.h>
31 #include <ac/string.h>
43 struct berval dn = BER_BVNULL;
47 Modifications *modlist = NULL;
48 Modifications **modtail = &modlist;
50 char textbuf[ SLAP_TEXT_BUFLEN ];
51 size_t textlen = sizeof( textbuf );
53 Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
56 * Parse the modify request. It looks like this:
58 * ModifyRequest := [APPLICATION 6] SEQUENCE {
59 * name DistinguishedName,
60 * mods SEQUENCE OF SEQUENCE {
61 * operation ENUMERATED {
66 * modification SEQUENCE {
68 * values SET OF AttributeValue
74 if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
75 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
77 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
78 return SLAPD_DISCONNECT;
81 Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
83 /* collect modifications & save for later */
84 for ( tag = ber_first_element( op->o_ber, &len, &last );
86 tag = ber_next_element( op->o_ber, &len, last ) )
89 Modifications tmp, *mod;
91 tmp.sml_nvalues = NULL;
93 if ( ber_scanf( op->o_ber, "{e{m[W]}}", &mop,
94 &tmp.sml_type, &tmp.sml_values ) == LBER_ERROR )
96 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR,
97 "decoding modlist error" );
98 rs->sr_err = SLAPD_DISCONNECT;
102 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
105 mod->sml_type = tmp.sml_type;
106 mod->sml_values = tmp.sml_values;
107 mod->sml_nvalues = NULL;
108 mod->sml_desc = NULL;
109 mod->sml_next = NULL;
114 if ( mod->sml_values == NULL ) {
115 Debug( LDAP_DEBUG_ANY,
116 "do_modify: modify/add operation (%ld) requires values\n",
119 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
120 "modify/add operation requires values" );
126 case LDAP_MOD_DELETE:
127 case LDAP_MOD_REPLACE:
130 case LDAP_MOD_INCREMENT:
131 if( op->o_protocol >= LDAP_VERSION3 ) {
133 if ( mod->sml_values == NULL ) {
134 Debug( LDAP_DEBUG_ANY, "do_modify: "
135 "modify/increment operation (%ld) requires value\n",
138 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
139 "modify/increment operation requires value" );
143 if ( !BER_BVISNULL( &mod->sml_values[ 1 ] ) ) {
144 Debug( LDAP_DEBUG_ANY, "do_modify: modify/increment "
145 "operation (%ld) requires single value\n",
148 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
149 "modify/increment operation requires single value" );
158 Debug( LDAP_DEBUG_ANY,
159 "do_modify: unrecognized modify operation (%ld)\n",
162 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
163 "unrecognized modify operation" );
168 modtail = &mod->sml_next;
172 if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
173 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
177 rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
179 if( rs->sr_err != LDAP_SUCCESS ) {
180 Debug( LDAP_DEBUG_ANY,
181 "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
182 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
186 rs->sr_err = slap_mods_check( op, modlist,
187 &rs->sr_text, textbuf, textlen, NULL );
189 if ( rs->sr_err != LDAP_SUCCESS ) {
190 send_ldap_result( op, rs );
194 /* FIXME: needs review */
195 op->orm_modlist = modlist;
196 op->orm_increment = increment;
198 op->o_bd = frontendDB;
199 rs->sr_err = frontendDB->be_modify( op, rs );
202 if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
209 op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
210 op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
211 if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
217 fe_op_modify( Operation *op, SlapReply *rs )
223 BackendDB *op_be, *bd = op->o_bd;
224 char textbuf[ SLAP_TEXT_BUFLEN ];
225 size_t textlen = sizeof( textbuf );
227 if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
228 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
230 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
231 "modify upon the root DSE not supported" );
234 } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
235 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
237 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
238 "modification of subschema subentry not supported" );
243 Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
245 for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
246 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
247 tmp->sml_op == LDAP_MOD_ADD ? "add" :
248 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
249 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
250 "replace")), tmp->sml_type.bv_val, 0 );
252 if ( tmp->sml_values == NULL ) {
253 Debug( LDAP_DEBUG_ARGS, "%s\n",
254 "\t\tno values", NULL, NULL );
255 } else if ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
256 Debug( LDAP_DEBUG_ARGS, "%s\n",
257 "\t\tzero values", NULL, NULL );
258 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
259 Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
260 "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
262 Debug( LDAP_DEBUG_ARGS, "%s\n",
263 "\t\tmultiple values", NULL, NULL );
267 if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
268 char abuf[BUFSIZ/2], *ptr = abuf;
271 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
272 op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
274 for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
275 if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
276 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
277 op->o_log_prefix, abuf, 0, 0, 0 );
282 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
283 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
284 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
292 ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
293 len += tmp->sml_type.bv_len;
296 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
297 op->o_log_prefix, abuf, 0, 0, 0 );
300 #endif /* LDAP_DEBUG */
302 manageDSAit = get_manageDSAit( op );
305 * We could be serving multiple database backends. Select the
306 * appropriate one, or send a referral to our "referral server"
307 * if we don't hold it.
309 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
310 if ( op->o_bd == NULL ) {
312 rs->sr_ref = referral_rewrite( default_referral,
313 NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
315 rs->sr_ref = default_referral;
318 if ( rs->sr_ref != NULL ) {
319 rs->sr_err = LDAP_REFERRAL;
320 send_ldap_result( op, rs );
322 if ( rs->sr_ref != default_referral ) {
323 ber_bvarray_free( rs->sr_ref );
327 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
328 "no global superior knowledge" );
333 /* If we've got a glued backend, check the real backend */
335 if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
336 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
339 /* check restrictions */
340 if ( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
341 send_ldap_result( op, rs );
345 /* check for referrals */
346 if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
350 rs->sr_err = slap_mods_obsolete_check( op, op->orm_modlist,
351 &rs->sr_text, textbuf, textlen );
352 if ( rs->sr_err != LDAP_SUCCESS ) {
353 send_ldap_result( op, rs );
357 /* check for modify/increment support */
358 if ( op->orm_increment && !SLAP_INCREMENT( op->o_bd ) ) {
359 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
360 "modify/increment not supported in context" );
364 * do the modify if 1 && (2 || 3)
365 * 1) there is a modify function implemented in this backend;
366 * 2) this backend is master for what it holds;
367 * 3) it's a replica and the dn supplied is the update_ndn.
369 if ( op->o_bd->be_modify ) {
370 /* do the update here */
371 int repl_user = be_isupdate( op );
374 * Multimaster slapd does not have to check for replicator dn
375 * because it accepts each modify request
377 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
378 int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
379 slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
384 rs->sr_err = slap_mods_no_user_mod_check( op, op->orm_modlist,
385 &rs->sr_text, textbuf, textlen );
386 if ( rs->sr_err != LDAP_SUCCESS ) {
387 send_ldap_result( op, rs );
393 /* but multimaster slapd logs only the ones
394 * not from a replicator user */
395 cb.sc_next = op->o_callback;
396 op->o_callback = &cb;
398 op->o_bd->be_modify( op, rs );
400 } else { /* send a referral */
401 BerVarray defref = op->o_bd->be_update_refs
402 ? op->o_bd->be_update_refs : default_referral;
403 if ( defref != NULL ) {
404 rs->sr_ref = referral_rewrite( defref,
406 LDAP_SCOPE_DEFAULT );
407 if ( rs->sr_ref == NULL ) {
408 /* FIXME: must duplicate, because
409 * overlays may muck with it */
412 rs->sr_err = LDAP_REFERRAL;
413 send_ldap_result( op, rs );
414 if ( rs->sr_ref != defref ) {
415 ber_bvarray_free( rs->sr_ref );
419 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
420 "shadow context; no update referral" );
425 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
426 "operation not supported within namingContext" );
435 * Obsolete constraint checking.
438 slap_mods_obsolete_check(
445 if( get_relax( op ) ) return LDAP_SUCCESS;
447 for ( ; ml != NULL; ml = ml->sml_next ) {
448 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
449 (( ml->sml_op != LDAP_MOD_REPLACE &&
450 ml->sml_op != LDAP_MOD_DELETE ) ||
451 ml->sml_values != NULL ))
454 * attribute is obsolete,
455 * only allow replace/delete with no values
457 snprintf( textbuf, textlen,
458 "%s: attribute is obsolete",
459 ml->sml_type.bv_val );
461 return LDAP_CONSTRAINT_VIOLATION;
469 * No-user-modification constraint checking.
472 slap_mods_no_user_mod_check(
479 for ( ; ml != NULL; ml = ml->sml_next ) {
480 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
484 if ( get_relax( op ) ) {
485 if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
486 ml->sml_flags |= SLAP_MOD_MANAGING;
490 /* attribute not manageable */
491 snprintf( textbuf, textlen,
492 "%s: no-user-modification attribute not manageable",
493 ml->sml_type.bv_val );
496 /* user modification disallowed */
497 snprintf( textbuf, textlen,
498 "%s: no user modification allowed",
499 ml->sml_type.bv_val );
503 return LDAP_CONSTRAINT_VIOLATION;
510 slap_mods_no_repl_user_mod_check(
520 for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
521 assert( mods->sml_op == LDAP_MOD_ADD );
523 /* check doesn't already appear */
524 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
525 if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
526 snprintf( textbuf, textlen,
527 "attribute '%s' provided more than once",
528 mods->sml_desc->ad_cname.bv_val );
530 return LDAP_TYPE_OR_VALUE_EXISTS;
539 * Do basic attribute type checking and syntax validation.
551 for( ; ml != NULL; ml = ml->sml_next ) {
552 AttributeDescription *ad = NULL;
554 /* convert to attribute description */
555 if ( ml->sml_desc == NULL ) {
556 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
557 if( rc != LDAP_SUCCESS ) {
558 snprintf( textbuf, textlen, "%s: %s",
559 ml->sml_type.bv_val, *text );
567 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
568 && !slap_ad_is_binary( ad ))
570 /* attribute requires binary transfer */
571 snprintf( textbuf, textlen,
572 "%s: requires ;binary transfer",
573 ml->sml_type.bv_val );
575 return LDAP_UNDEFINED_TYPE;
578 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
579 && slap_ad_is_binary( ad ))
581 /* attribute does not require binary transfer */
582 snprintf( textbuf, textlen,
583 "%s: disallows ;binary transfer",
584 ml->sml_type.bv_val );
586 return LDAP_UNDEFINED_TYPE;
589 if( slap_ad_is_tag_range( ad )) {
590 /* attribute requires binary transfer */
591 snprintf( textbuf, textlen,
592 "%s: inappropriate use of tag range option",
593 ml->sml_type.bv_val );
595 return LDAP_UNDEFINED_TYPE;
599 if ( is_at_obsolete( ad->ad_type ) &&
600 (( ml->sml_op != LDAP_MOD_REPLACE &&
601 ml->sml_op != LDAP_MOD_DELETE ) ||
602 ml->sml_values != NULL ))
605 * attribute is obsolete,
606 * only allow replace/delete with no values
608 snprintf( textbuf, textlen,
609 "%s: attribute is obsolete",
610 ml->sml_type.bv_val );
612 return LDAP_CONSTRAINT_VIOLATION;
616 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
617 #ifdef SLAPD_REAL_SYNTAX
618 !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
620 !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
623 * attribute values must be INTEGER or REAL
625 snprintf( textbuf, textlen,
626 "%s: attribute syntax inappropriate for increment",
627 ml->sml_type.bv_val );
629 return LDAP_CONSTRAINT_VIOLATION;
635 if( ml->sml_values != NULL ) {
637 slap_syntax_validate_func *validate =
638 ad->ad_type->sat_syntax->ssyn_validate;
639 slap_syntax_transform_func *pretty =
640 ad->ad_type->sat_syntax->ssyn_pretty;
642 if( !pretty && !validate ) {
643 *text = "no validator for syntax";
644 snprintf( textbuf, textlen,
645 "%s: no validator for syntax %s",
647 ad->ad_type->sat_syntax->ssyn_oid );
649 return LDAP_INVALID_SYNTAX;
653 * check that each value is valid per syntax
654 * and pretty if appropriate
656 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
660 rc = ordered_value_pretty( ad,
661 &ml->sml_values[nvals], &pval, ctx );
663 rc = ordered_value_validate( ad,
664 &ml->sml_values[nvals], ml->sml_op );
668 snprintf( textbuf, textlen,
669 "%s: value #%ld invalid per syntax",
670 ml->sml_type.bv_val, (long) nvals );
672 return LDAP_INVALID_SYNTAX;
676 ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
677 ml->sml_values[nvals] = pval;
682 * a rough single value check... an additional check is needed
683 * to catch add of single value to existing single valued attribute
685 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
686 && nvals > 1 && is_at_single_value( ad->ad_type ))
688 snprintf( textbuf, textlen,
689 "%s: multiple values provided",
690 ml->sml_type.bv_val );
692 return LDAP_CONSTRAINT_VIOLATION;
695 /* if the type has a normalizer, generate the
696 * normalized values. otherwise leave them NULL.
698 * this is different from the rule for attributes
699 * in an entry - in an attribute list, the normalized
700 * value is set equal to the non-normalized value
701 * when there is no normalizer.
703 if( nvals && ad->ad_type->sat_equality &&
704 ad->ad_type->sat_equality->smr_normalize )
706 ml->sml_nvalues = ber_memalloc_x(
707 (nvals+1)*sizeof(struct berval), ctx );
709 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
710 rc = ordered_value_normalize(
711 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
713 ad->ad_type->sat_equality,
714 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
716 Debug( LDAP_DEBUG_ANY,
717 "<= str2entry NULL (ssyn_normalize %d)\n",
719 snprintf( textbuf, textlen,
720 "%s: value #%ld normalization failed",
721 ml->sml_type.bv_val, (long) nvals );
727 BER_BVZERO( &ml->sml_nvalues[nvals] );
730 /* check for duplicates, but ignore Deletes.
732 if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
733 #define SLAP_MODS_CHECK_QUICKSORT
734 #ifndef SLAP_MODS_CHECK_QUICKSORT
736 MatchingRule *mr = ad->ad_type->sat_equality;
738 for ( i = 1; i < nvals ; i++ ) {
739 /* test asserted values against themselves */
740 for( j = 0; j < i; j++ ) {
741 rc = ordered_value_match( &match, ml->sml_desc, mr,
743 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
744 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
745 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
747 ? &ml->sml_nvalues[i]
748 : &ml->sml_values[i],
750 ? &ml->sml_nvalues[j]
751 : &ml->sml_values[j],
753 if ( rc == LDAP_SUCCESS && match == 0 ) {
754 /* value exists already */
755 snprintf( textbuf, textlen,
756 "%s: value #%d provided more than once",
757 ml->sml_desc->ad_cname.bv_val, j );
759 return LDAP_TYPE_OR_VALUE_EXISTS;
761 } else if ( rc != LDAP_SUCCESS ) {
766 #else /* SLAP_MODS_CHECK_QUICKSORT */
768 /* Quicksort + Insertion sort for small arrays */
771 #define SWAP(a,b,tmp) tmp=(a);(a)=(b);(b)=tmp
772 #define COMP(a,b) match=0; rc = ordered_value_match( &match, \
773 ml->sml_desc, mr, SLAP_MR_EQUALITY \
774 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX \
775 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH \
776 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH, \
779 MatchingRule *mr = ad->ad_type->sat_equality;
780 int istack[sizeof(int)*16];
781 int i,j,k,l,ir,jstack, rc, match, *ix, itmp;
782 struct berval a, *cv;
784 /* If PRESERVE_ORDER is defined only the index array is sorted; the
785 * actual values are left in their incoming order. Otherwise, the
786 * only reason to keep the index array is to identify the offending
787 * value when duplicates are found.
789 #define PRESERVE_ORDER
790 #ifndef PRESERVE_ORDER
791 struct berval va, *v, *nv, bvtmp;
794 #define EXCH(x,y) SWAP(ix[x],ix[y],itmp); SWAP(cv[x],cv[y],bvtmp); \
795 if (nv) {SWAP(v[x],v[y],bvtmp);}
796 #define SETA(x) itmp = ix[x]; a = cv[x]; if (nv) va=v[x]
797 #define GETA(x) ix[x] = itmp; cv[x] = a; if (nv) v[x]=va
798 #define SET(x,y) ix[x] = ix[y]; cv[x] = cv[y]; if (nv) v[x]=v[y]
801 nv = ml->sml_nvalues;
803 #else /* PRESERVE_ORDER */
806 #define EXCH(x,y) SWAP(ix[x],ix[y],itmp)
807 #define SETA(x) itmp = ix[x]; a = cv[itmp]
808 #define GETA(x) ix[x] = itmp;
809 #define SET(x,y) ix[x] = ix[y]
811 #endif /* PRESERVE_ORDER */
813 cv = ml->sml_nvalues ? ml->sml_nvalues : ml->sml_values;
814 if ( ad == slap_schema.si_ad_objectClass )
815 mr = NULL; /* shortcut matching */
817 /* record indices to preserve input ordering */
818 ix = slap_sl_malloc( nvals * sizeof(int), ctx );
819 for (i=0; i<nvals; i++) ix[i] = i;
826 if (ir - l < SMALL) { /* Insertion sort */
828 for (j=l+1;j<=ir;j++) {
830 for (i=j-1;i>=0;i--) {
837 if ( match == 0 ) goto done;
839 if ( jstack == 0 ) break;
840 if ( match == 0 ) break;
841 ir = istack[jstack--];
842 l = istack[jstack--];
844 k = (l + ir) >> 1; /* Choose median of left, center, right */
846 COMP( cv[IX(l)], cv[IX(ir)] );
849 } else if ( match == 0 ) {
853 COMP( cv[IX(l+1)], cv[IX(ir)] );
856 } else if ( match == 0 ) {
860 COMP( cv[IX(l)], cv[IX(l+1)] );
863 } else if ( match == 0 ) {
873 COMP( cv[IX(i)], a );
874 } while( match < 0 );
877 COMP( cv[IX(j)], a );
895 istack[jstack-1] = i;
899 istack[jstack-1] = l;
908 slap_sl_free( ix, ctx );
910 if ( rc != LDAP_SUCCESS ) {
912 } else if ( match == 0 ) {
913 /* value exists already */
916 snprintf( textbuf, textlen,
917 "%s: value #%d provided more than once",
918 ml->sml_desc->ad_cname.bv_val, j );
920 return LDAP_TYPE_OR_VALUE_EXISTS;
922 #endif /* SLAP_MODS_CHECK_QUICKSORT */
930 /* Enter with bv->bv_len = sizeof buffer, returns with
931 * actual length of string
933 void slap_timestamp( time_t *tm, struct berval *bv )
939 ltm = gmtime_r( tm, <m_buf );
941 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
945 bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
947 #ifndef HAVE_GMTIME_R
948 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
952 /* Called for all modify and modrdn ops. If the current op was replicated
953 * from elsewhere, all of the attrs should already be present.
955 void slap_mods_opattrs(
957 Modifications **modsp,
960 struct berval name, timestamp, csn = BER_BVNULL;
962 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
963 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
964 Modifications *mod, **modtail, *modlast;
965 int gotcsn = 0, gotmname = 0, gotmtime = 0;
967 if ( SLAP_LASTMOD( op->o_bd ) ) {
969 timestamp.bv_val = timebuf;
970 for ( modtail = modsp; *modtail; modtail = &(*modtail)->sml_next ) {
971 if ( (*modtail)->sml_op != LDAP_MOD_ADD &&
972 (*modtail)->sml_op != LDAP_MOD_REPLACE ) continue;
973 if ( (*modtail)->sml_desc == slap_schema.si_ad_entryCSN ) {
974 csn = (*modtail)->sml_values[0];
977 if ( (*modtail)->sml_desc == slap_schema.si_ad_modifiersName ) {
980 if ( (*modtail)->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
984 if ( BER_BVISEMPTY( &op->o_csn )) {
987 csn.bv_len = sizeof( csnbuf );
988 slap_get_csn( op, &csn, manage_ctxcsn );
991 slap_queue_csn( op, &csn );
996 ptr = ber_bvchr( &csn, '#' );
998 timestamp.bv_len = ptr - csn.bv_val;
999 if ( timestamp.bv_len >= sizeof( timebuf )) /* ?!? */
1000 timestamp.bv_len = sizeof( timebuf ) - 1;
1001 AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
1002 timebuf[timestamp.bv_len] = '\0';
1004 time_t now = slap_get_time();
1006 timestamp.bv_len = sizeof(timebuf);
1008 slap_timestamp( &now, ×tamp );
1011 if ( BER_BVISEMPTY( &op->o_dn ) ) {
1012 BER_BVSTR( &name, SLAPD_ANONYMOUS );
1020 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1021 mod->sml_op = LDAP_MOD_REPLACE;
1022 mod->sml_flags = SLAP_MOD_INTERNAL;
1023 mod->sml_next = NULL;
1024 BER_BVZERO( &mod->sml_type );
1025 mod->sml_desc = slap_schema.si_ad_entryCSN;
1026 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1027 ber_dupbv( &mod->sml_values[0], &csn );
1028 BER_BVZERO( &mod->sml_values[1] );
1029 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1030 mod->sml_nvalues = NULL;
1033 modtail = &mod->sml_next;
1037 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1038 mod->sml_op = LDAP_MOD_REPLACE;
1039 mod->sml_flags = SLAP_MOD_INTERNAL;
1040 mod->sml_next = NULL;
1041 BER_BVZERO( &mod->sml_type );
1042 mod->sml_desc = slap_schema.si_ad_modifiersName;
1043 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1044 ber_dupbv( &mod->sml_values[0], &name );
1045 BER_BVZERO( &mod->sml_values[1] );
1046 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1048 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1049 ber_dupbv( &mod->sml_nvalues[0], &nname );
1050 BER_BVZERO( &mod->sml_nvalues[1] );
1051 assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
1053 modtail = &mod->sml_next;
1057 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1058 mod->sml_op = LDAP_MOD_REPLACE;
1059 mod->sml_flags = SLAP_MOD_INTERNAL;
1060 mod->sml_next = NULL;
1061 BER_BVZERO( &mod->sml_type );
1062 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1063 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1064 ber_dupbv( &mod->sml_values[0], ×tamp );
1065 BER_BVZERO( &mod->sml_values[1] );
1066 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1067 mod->sml_nvalues = NULL;
1069 modtail = &mod->sml_next;