]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-meta/compare.c
minor cleanup
[openldap] / servers / slapd / back-meta / compare.c
index fd009c57dc4d27e297889247ddcfc887051a6b1e..65fdde05f01c50c9a084b29dacd6ba974f750884 100644 (file)
@@ -1,67 +1,23 @@
-/*
- * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* $OpenLDAP$ */
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
+ * Copyright 1999-2005 The OpenLDAP Foundation.
+ * Portions Copyright 2001-2003 Pierangelo Masarati.
+ * Portions Copyright 1999-2003 Howard Chu.
+ * All rights reserved.
  *
- * This work has been developed to fulfill the requirements
- * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
- * to the OpenLDAP Foundation in the hope that it may be useful
- * to the Open Source community, but WITHOUT ANY WARRANTY.
- *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
- *
- * 1. The author and SysNet s.n.c. are not responsible for the consequences
- *    of use of this software, no matter how awful, even if they arise from 
- *    flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- *    explicit claim or by omission.  Since few users ever read sources,
- *    credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.  Since few users
- *    ever read sources, credits should appear in the documentation.
- *    SysNet s.n.c. cannot be responsible for the consequences of the
- *    alterations.
- * 
- * 4. This notice may not be removed or altered.
- *
- *
- * This software is based on the backend back-ldap, implemented
- * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
- * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
- * contributors. The contribution of the original software to the present
- * implementation is acknowledged in this copyright statement.
- *
- * A special acknowledgement goes to Howard for the overall architecture
- * (and for borrowing large pieces of code), and to Mark, who implemented
- * from scratch the attribute/objectclass mapping.
- *
- * The original copyright statement follows.
- *
- * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
- *
- * Permission is granted to anyone to use this software for any purpose
- * on any computer system, and to alter it and redistribute it, subject
- * to the following restrictions:
- *
- * 1. The author is not responsible for the consequences of use of this
- *    software, no matter how awful, even if they arise from flaws in it.
- *
- * 2. The origin of this software must not be misrepresented, either by
- *    explicit claim or by omission.  Since few users ever read sources,
- *    credits should appear in the documentation.
- *
- * 3. Altered versions must be plainly marked as such, and must not be
- *    misrepresented as being the original software.  Since few users
- *    ever read sources, credits should appear in the
- *    documentation.
- *
- * 4. This notice may not be removed or altered.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted only as authorized by the OpenLDAP
+ * Public License.
  *
+ * A copy of this license is available in the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
+ */
+/* ACKNOWLEDGEMENTS:
+ * This work was initially developed by the Howard Chu for inclusion
+ * in OpenLDAP Software and subsequently enhanced by Pierangelo
+ * Masarati.
  */
 
 #include "portable.h"
 #include "back-meta.h"
 
 int
-meta_back_compare(
-               Backend                 *be,
-               Connection              *conn,
-               Operation               *op,
-               struct berval           *dn,
-               struct berval           *ndn,
-               AttributeAssertion      *ava
-)
+meta_back_compare( Operation *op, SlapReply *rs )
 {
-       struct metainfo *li = ( struct metainfo * )be->be_private;
-       struct metaconn *lc;
-       struct metasingleconn **lsc;
-       char *match = NULL, *err = NULL, *mmatch = NULL;
-       int candidates = 0, last = 0, i, count, rc;
-               int cres = LDAP_SUCCESS, rres = LDAP_SUCCESS;
-       int *msgid;
-
-       lc = meta_back_getconn( li, conn, op, META_OP_ALLOW_MULTIPLE,
-                       ndn, NULL );
-       if ( !lc || !meta_back_dobind( lc, op ) ) {
-               return -1;
-       }
+       metainfo_t              *mi = ( metainfo_t * )op->o_bd->be_private;
+       metaconn_t              *mc = NULL;
+       char                    *match = NULL,
+                               *err = NULL;
+       struct berval           mmatch = BER_BVNULL;
+       int                     ncandidates = 0,
+                               last = 0,
+                               i,
+                               count = 0,
+                               rc,
+                                       cres = LDAP_SUCCESS,
+                               rres = LDAP_SUCCESS,
+                               *msgid;
+       dncookie                dc;
+
+       SlapReply               *candidates = meta_back_candidates_get( op );
 
-       msgid = ch_calloc( sizeof( int ), li->ntargets );
+       mc = meta_back_getconn( op, rs, NULL, LDAP_BACK_SENDERR );
+       if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
+               return rs->sr_err;
+       }
+       
+       msgid = ch_calloc( sizeof( int ), mi->mi_ntargets );
        if ( msgid == NULL ) {
-               return -1;
+               send_ldap_error( op, rs, LDAP_OTHER, NULL );
+               rc = LDAP_OTHER;
+               goto done;
        }
 
        /*
         * start an asynchronous compare for each candidate target
         */
-       for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; ++i, ++lsc ) {
-               char *mdn = NULL;
-               char *mapped_attr = ava->aa_desc->ad_cname.bv_val;
-               char *mapped_value = ava->aa_value->bv_val;
+       dc.conn = op->o_conn;
+       dc.rs = rs;
+       dc.ctx = "compareDN";
 
-               if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
+       for ( i = 0; i < mi->mi_ntargets; i++ ) {
+               struct berval           mdn = BER_BVNULL;
+               struct berval           mapped_attr = op->orc_ava->aa_desc->ad_cname;
+               struct berval           mapped_value = op->orc_ava->aa_value;
+
+               if ( candidates[ i ].sr_tag != META_CANDIDATE ) {
+                       msgid[ i ] = -1;
                        continue;
                }
 
                /*
                 * Rewrite the compare dn, if needed
                 */
-               switch ( rewrite_session( li->targets[ i ]->rwinfo,
-                                       "compareDn", 
-                                       dn->bv_val, conn, &mdn ) ) {
-               case REWRITE_REGEXEC_OK:
-                       if ( mdn == NULL ) {
-                               mdn = ( char * )dn->bv_val;
-                       }
-#ifdef NEW_LOGGING
-                       LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
-                                       "[rw] compareDn: \"%s\" -> \"%s\"\n",
-                                       dn->bv_val, mdn ));
-#else /* !NEW_LOGGING */
-                       Debug( LDAP_DEBUG_ARGS,
-                                       "rw> compareDn: \"%s\" -> \"%s\"\n%s",
-                                       dn->bv_val, mdn, "" );
-#endif /* !NEW_LOGGING */
+               dc.target = &mi->mi_targets[ i ];
+
+               switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
+               case LDAP_UNWILLING_TO_PERFORM:
+                       rc = 1;
+                       goto finish;
+
+               default:
                        break;
-               
-               case REWRITE_REGEXEC_UNWILLING:
-                       send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, "Unwilling to perform",
-                                       NULL, NULL );
-                       return -1;
-                       
-               case REWRITE_REGEXEC_ERR:
-                       send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
-                                       NULL, "Operations error",
-                                       NULL, NULL );
-                       return -1;
                }
 
                /*
                 * if attr is objectClass, try to remap the value
                 */
-               if ( ava->aa_desc->ad_type->sat_oid 
-                       == slap_schema.si_ad_objectClass->ad_type->sat_oid ) {
-                       mapped_value = ldap_back_map( &li->targets[ i ]->oc_map,
-                                       ava->aa_value->bv_val, 0 );
+               if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
+                       ldap_back_map( &mi->mi_targets[ i ].mt_rwmap.rwm_oc,
+                                       &op->orc_ava->aa_value,
+                                       &mapped_value, BACKLDAP_MAP );
 
-                       if ( mapped_value == NULL ) {
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
+                       if ( BER_BVISNULL( &mapped_value ) || mapped_value.bv_val[0] == '\0' ) {
                                continue;
                        }
                /*
                 * else try to remap the attribute
                 */
                } else {
-                       mapped_attr = ldap_back_map( &li->targets[ i ]->at_map,
-                               ava->aa_desc->ad_cname.bv_val, 0 );
-                       if ( mapped_attr == NULL ) {
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
+                       ldap_back_map( &mi->mi_targets[ i ].mt_rwmap.rwm_at,
+                               &op->orc_ava->aa_desc->ad_cname,
+                               &mapped_attr, BACKLDAP_MAP );
+                       if ( BER_BVISNULL( &mapped_attr ) || mapped_attr.bv_val[0] == '\0' ) {
                                continue;
                        }
+
+                       if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
+                       {
+                               dc.ctx = "compareAttrDN";
+
+                               switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
+                               {
+                               case LDAP_UNWILLING_TO_PERFORM:
+                                       rc = 1;
+                                       goto finish;
+
+                               default:
+                                       break;
+                               }
+                       }
                }
                
                /*
@@ -179,72 +137,96 @@ meta_back_compare(
                 * that returns determines the result; a constraint on unicity
                 * of the result ought to be enforced
                 */
-               msgid[ i ] = ldap_compare( lc->conns[ i ]->ld, mdn,
-                               mapped_attr, mapped_value );
-               if ( msgid[ i ] == -1 ) {
-                       lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
-                       continue;
+                rc = ldap_compare_ext( mc->mc_conns[ i ].msc_ld, mdn.bv_val,
+                               mapped_attr.bv_val, &mapped_value,
+                               op->o_ctrls, NULL, &msgid[ i ] );
+
+               if ( mdn.bv_val != op->o_req_dn.bv_val ) {
+                       free( mdn.bv_val );
+                       BER_BVZERO( &mdn );
                }
 
-               if ( mdn != dn->bv_val ) {
-                       free( mdn );
+               if ( mapped_attr.bv_val != op->orc_ava->aa_desc->ad_cname.bv_val ) {
+                       free( mapped_attr.bv_val );
+                       BER_BVZERO( &mapped_attr );
                }
-               if ( mapped_attr != ava->aa_desc->ad_cname.bv_val ) {
-                       free( mapped_attr );
+
+               if ( mapped_value.bv_val != op->orc_ava->aa_value.bv_val ) {
+                       free( mapped_value.bv_val );
+                       BER_BVZERO( &mapped_value );
                }
-               if ( mapped_value != ava->aa_value->bv_val ) {
-                       free( mapped_value );
+
+               if ( rc != LDAP_SUCCESS ) {
+                       /* FIXME: what should we do with the error? */
+                       continue;
                }
 
-               ++candidates;
+               ++ncandidates;
        }
 
        /*
         * wait for replies
         */
-       for ( rc = 0, count = 0; candidates > 0; ) {
+       for ( rc = 0, count = 0; ncandidates > 0; ) {
 
                /*
                 * FIXME: should we check for abandon?
                 */
-               for ( i = 0, lsc = lc->conns; lsc[ 0 ] != NULL; lsc++, i++ ) {
-                       int lrc;
-                       LDAPMessage *res = NULL;
+               for ( i = 0; i < mi->mi_ntargets; i++ ) {
+                       metasingleconn_t        *msc = &mc->mc_conns[ i ];
+                       int                     lrc;
+                       LDAPMessage             *res = NULL;
+                       struct timeval          tv;
+
+                       LDAP_BACK_TV_SET( &tv );
 
-                       if ( lsc[ 0 ]->candidate != META_CANDIDATE ) {
+                       if ( msgid[ i ] == -1 ) {
                                continue;
                        }
 
-                       lrc = ldap_result( lsc[ 0 ]->ld, msgid[ i ],
-                                       0, NULL, &res );
+                       lrc = ldap_result( msc->msc_ld, msgid[ i ],
+                                       0, &tv, &res );
 
                        if ( lrc == 0 ) {
-                               /*
-                                * FIXME: should we yield?
-                                */
-                               if ( res ) {
-                                       ldap_msgfree( res );
-                               }
+                               assert( res == NULL );
                                continue;
+
+                       } else if ( lrc == -1 ) {
+                               /* we do not retry in this case;
+                                * only for unique operations... */
+                               ldap_get_option( msc->msc_ld,
+                                       LDAP_OPT_ERROR_NUMBER, &rs->sr_err );
+                               rres = slap_map_api2result( rs );
+                               rres = rc;
+                               rc = -1;
+                               goto finish;
+
                        } else if ( lrc == LDAP_RES_COMPARE ) {
                                if ( count > 0 ) {
-                                       rres = LDAP_OPERATIONS_ERROR;
+                                       rres = LDAP_OTHER;
+                                       rc = -1;
+                                       goto finish;
+                               }
+
+                               rc = ldap_parse_result( msc->msc_ld, res,
+                                               &rs->sr_err,
+                                               NULL, NULL, NULL, NULL, 1 );
+                               if ( rc != LDAP_SUCCESS ) {
+                                       rres = rc;
                                        rc = -1;
                                        goto finish;
                                }
                                
-                               cres = ldap_result2error( lsc[ 0 ]->ld,
-                                               res, 1 );
-                               switch ( cres ) {
+                               switch ( rs->sr_err ) {
                                case LDAP_COMPARE_TRUE:
                                case LDAP_COMPARE_FALSE:
 
                                        /*
-                                        * true or flase, got it;
+                                        * true or false, got it;
                                         * sending to cache ...
                                         */
-                                       if ( li->cache.ttl != META_DNCACHE_DISABLED ) {
-                                               ( void )meta_dncache_update_entry( &li->cache, ber_bvdup( ndn ), i );
+                                       if ( mi->mi_cache.ttl != META_DNCACHE_DISABLED ) {
+                                               ( void )meta_dncache_update_entry( &mi->mi_cache, &op->o_req_ndn, i );
                                        }
 
                                        count++;
@@ -252,28 +234,29 @@ meta_back_compare(
                                        break;
 
                                default:
-                                       rres = ldap_back_map_result( cres );
+                                       rres = slap_map_api2result( rs );
 
                                        if ( err != NULL ) {
                                                free( err );
                                        }
-                                       ldap_get_option( lsc[ 0 ]->ld,
+                                       ldap_get_option( msc->msc_ld,
                                                LDAP_OPT_ERROR_STRING, &err );
 
                                        if ( match != NULL ) {
                                                free( match );
                                        }
-                                       ldap_get_option( lsc[ 0 ]->ld,
+                                       ldap_get_option( msc->msc_ld,
                                                LDAP_OPT_MATCHED_DN, &match );
                                        
                                        last = i;
                                        break;
                                }
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
-                               --candidates;
+                               msgid[ i ] = -1;
+                               --ncandidates;
+
                        } else {
-                               lsc[ 0 ]->candidate = META_NOT_CANDIDATE;
-                               --candidates;
+                               msgid[ i ] = -1;
+                               --ncandidates;
                                if ( res ) {
                                        ldap_msgfree( res );
                                }
@@ -301,52 +284,35 @@ finish:;
                 */
                rres = cres;
                
-       } else if ( match != NULL ) {
-               
                /*
                 * At least one compare failed with matched portion,
                 * and none was successful
                 */
-               switch ( rewrite_session( li->targets[ last ]->rwinfo,
-                                       "matchedDn", match, conn, &mmatch ) ) {
-               case REWRITE_REGEXEC_OK:
-                       if ( mmatch == NULL ) {
-                               mmatch = ( char * )match;
+       } else if ( match != NULL && match[ 0 ] != '\0' ) {
+               struct berval matched, pmatched;
+
+               ber_str2bv( match, 0, 0, &matched );
+
+               dc.ctx = "matchedDN";
+               ldap_back_dn_massage( &dc, &matched, &mmatch );
+               if ( dnPretty( NULL, &mmatch, &pmatched, NULL ) == LDAP_SUCCESS ) {
+                       if ( mmatch.bv_val != match ) {
+                               free( mmatch.bv_val );
                        }
-#ifdef NEW_LOGGING
-                       LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
-                                       "[rw] matchedDn: \"%s\" -> \"%s\"\n",
-                                       match, mmatch ));
-#else /* !NEW_LOGGING */
-                       Debug( LDAP_DEBUG_ARGS, "rw> matchedDn:"
-                                       " \"%s\" -> \"%s\"\n%s",
-                                       match, mmatch, "" );
-#endif /* !NEW_LOGGING */
-                       break;
-                       
-               
-               case REWRITE_REGEXEC_UNWILLING:
-                       send_ldap_result( conn, op, LDAP_UNWILLING_TO_PERFORM,
-                                       NULL, "Unwilling to perform",
-                                       NULL, NULL );
-                       rc = -1;
-                       goto cleanup;
-                       
-               case REWRITE_REGEXEC_ERR:
-                       send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR,
-                                       NULL, "Operations error",
-                                       NULL, NULL );
-                       rc = -1;
-                       goto cleanup;
+                       mmatch = pmatched;
                }
        }
 
-       send_ldap_result( conn, op, rres, mmatch, err, NULL, NULL );
+       if ( rres != LDAP_SUCCESS ) {
+               rs->sr_err = rres;
+       }
+       rs->sr_matched = mmatch.bv_val;
+       send_ldap_result( op, rs );
+       rs->sr_matched = NULL;
 
-cleanup:;
        if ( match != NULL ) {
-               if ( mmatch != match ) {
-                       free( mmatch );
+               if ( mmatch.bv_val != match ) {
+                       free( mmatch.bv_val );
                }
                free( match );
        }
@@ -354,7 +320,10 @@ cleanup:;
        if ( msgid ) {
                free( msgid );
        }
-       
+
+done:;
+       meta_back_release_conn( op, mc );
+
        return rc;
 }