]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/mods.c
allow backwards compatibility for 'T' option (single char)
[openldap] / servers / slapd / mods.c
index 8a40ef301a46fc5904e715034e36832c0f1f66ff..7844d6381e6fc55e86327418a3673f53c7e0c295 100644 (file)
@@ -37,11 +37,10 @@ modify_add_values(
        const char      **text,
        char *textbuf, size_t textlen )
 {
-       int             i, j;
-       int             matched;
-       Attribute       *a;
-       MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
+       int rc;
        const char *op;
+       Attribute *a;
+       Modification pmod = *mod;
 
        switch( mod->sm_op ) {
        case LDAP_MOD_ADD:
@@ -56,16 +55,12 @@ modify_add_values(
        }
 
        a = attr_find( e->e_attrs, mod->sm_desc );
+       if( a != NULL ) { /* check if values to add exist in attribute */
+               int     rc, i, j, p;
+               MatchingRule *mr;
 
-       /*
-        * With permissive set, as long as the attribute being added
-        * has the same value(s?) as the existing attribute, then the
-        * modify will succeed.
-        */
-
-       /* check if the values we're adding already exist */
-       if( mr == NULL || !mr->smr_match ) {
-               if ( a != NULL ) {
+               mr = mod->sm_desc->ad_type->sat_equality;
+               if( mr == NULL || !mr->smr_match ) {
                        /* do not allow add of additional attribute
                                if no equality rule exists */
                        *text = textbuf;
@@ -75,73 +70,74 @@ modify_add_values(
                        return LDAP_INAPPROPRIATE_MATCHING;
                }
 
-               for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
-                       /* test asserted values against existing values */
-                       for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
-                               if ( bvmatch( &mod->sm_values[i], &a->a_vals[j] ) ) {
-                                       if ( permissive ) {
-                                               matched++;
-                                               continue;
-                                       }
-                                       /* value exists already */
-                                       *text = textbuf;
-                                       snprintf( textbuf, textlen,
-                                               "modify/%s: %s: value #%i already exists",
-                                               op, mod->sm_desc->ad_cname.bv_val, j );
-                                       return LDAP_TYPE_OR_VALUE_EXISTS;
-                               }
-                       }
-
-                       if ( permissive && matched == j ) {
-                               /* values already exist; do nothing */
-                               return LDAP_SUCCESS;
+               if( permissive ) {
+                       for ( i=0; mod->sm_values[i].bv_val; i++ ) /* count 'em */;
+                       pmod.sm_values = (BerVarray) ch_malloc( i*sizeof( struct berval ));
+                       if( pmod.sm_nvalues != NULL ) {
+                               pmod.sm_nvalues = (BerVarray) ch_malloc(
+                                       i*sizeof( struct berval ));
                        }
                }
 
-       } else if ( a != NULL ) {
                /* no normalization is done in this routine nor
                 * in the matching routines called by this routine. 
                 * values are now normalized once on input to the
                 * server (whether from LDAP or from the underlying
                 * database).
                 */
-               int             rc;
+               for ( p=i=0; mod->sm_values[i].bv_val; i++ ) {
+                       int match;
+                       assert( a->a_vals[0].bv_val );
+                       for ( j=0; a->a_vals[j].bv_val; j++ ) {
+                               if( mod->sm_nvalues ) {
+                                       rc = value_match( &match, mod->sm_desc, mr,
+                                               SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
+                                                       | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
+                                                       | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
+                                               &a->a_nvals[j], &mod->sm_nvalues[i], text );
+                               } else {
+                                       rc = value_match( &match, mod->sm_desc, mr,
+                                               SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
+                                               &a->a_vals[j], &mod->sm_values[i], text );
+                               }
 
-               for ( matched = 0, i = 0; a->a_vals[i].bv_val; i++ ) {
-                       int     match;
+                               if( rc == LDAP_SUCCESS && match == 0 ) {
+                                       if( permissive ) break;
 
-                       if( mod->sm_nvalues ) {
-                               rc = value_match( &match, mod->sm_desc, mr,
-                                       SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
-                                               | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
-                                               | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
-                                       &a->a_nvals[i], &mod->sm_nvalues[0], text );
-                       } else {
-                               rc = value_match( &match, mod->sm_desc, mr,
-                                       SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
-                                       &a->a_vals[i], &mod->sm_values[0], text );
+                                       /* value already exists */
+                                       *text = textbuf;
+                                       snprintf( textbuf, textlen,
+                                               "modify/%s: %s: value #%d already exists",
+                                               op, mod->sm_desc->ad_cname.bv_val, i );
+                                       return LDAP_TYPE_OR_VALUE_EXISTS;
+                               }
                        }
 
-                       if( rc == LDAP_SUCCESS && match == 0 ) {
-                               if ( permissive ) {
-                                       matched++;
-                                       continue;
+                       if( permissive && !match ) {
+                               if( pmod.sm_nvalues ) {
+                                       pmod.sm_nvalues[p] = mod->sm_nvalues[i];
                                }
-                               *text = textbuf;
-                               snprintf( textbuf, textlen,
-                                       "modify/%s: %s: value #0 already exists",
-                                       op, mod->sm_desc->ad_cname.bv_val );
-                               return LDAP_TYPE_OR_VALUE_EXISTS;
+                               pmod.sm_values[p++] = mod->sm_values[i];
                        }
                }
-               if ( permissive && matched == i ) {
-                       /* values already exist; do nothing */
+
+               if( permissive && p == 0 ) {
+                       /* all new values match exist */
+                       ch_free( pmod.sm_values );
+                       if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
                        return LDAP_SUCCESS;
                }
        }
 
        /* no - add them */
-       if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
+       rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
+
+       if( a != NULL && permissive ) {
+               ch_free( pmod.sm_values );
+               if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
+       }
+
+       if( rc != 0 ) {
                /* this should return result of attr_merge */
                *text = textbuf;
                snprintf( textbuf, textlen,