Back to index

openldap  2.4.31
compare.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1999-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 2001-2003 Pierangelo Masarati.
00006  * Portions Copyright 1999-2003 Howard Chu.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by the Howard Chu for inclusion
00019  * in OpenLDAP Software and subsequently enhanced by Pierangelo
00020  * Masarati.
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #include <stdio.h>
00026 
00027 #include <ac/string.h>
00028 #include <ac/socket.h>
00029 
00030 #include "slap.h"
00031 #include "../back-ldap/back-ldap.h"
00032 #include "back-meta.h"
00033 
00034 int
00035 meta_back_compare( Operation *op, SlapReply *rs )
00036 {
00037        metainfo_t    *mi = ( metainfo_t * )op->o_bd->be_private;
00038        metatarget_t  *mt;
00039        metaconn_t    *mc;
00040        int           rc = 0;
00041        int           candidate = -1;
00042        struct berval mdn = BER_BVNULL;
00043        dncookie      dc;
00044        struct berval mapped_attr = op->orc_ava->aa_desc->ad_cname;
00045        struct berval mapped_value = op->orc_ava->aa_value;
00046        int           msgid;
00047        ldap_back_send_t     retrying = LDAP_BACK_RETRYING;
00048        LDAPControl   **ctrls = NULL;
00049 
00050        mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
00051        if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
00052               return rs->sr_err;
00053        }
00054 
00055        assert( mc->mc_conns[ candidate ].msc_ld != NULL );
00056 
00057        /*
00058         * Rewrite the modify dn, if needed
00059         */
00060        mt = mi->mi_targets[ candidate ];
00061        dc.target = mt;
00062        dc.conn = op->o_conn;
00063        dc.rs = rs;
00064        dc.ctx = "compareDN";
00065 
00066        switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
00067        case LDAP_UNWILLING_TO_PERFORM:
00068               rc = 1;
00069               goto cleanup;
00070 
00071        default:
00072               break;
00073        }
00074 
00075        /*
00076         * if attr is objectClass, try to remap the value
00077         */
00078        if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
00079               ldap_back_map( &mt->mt_rwmap.rwm_oc,
00080                             &op->orc_ava->aa_value,
00081                             &mapped_value, BACKLDAP_MAP );
00082 
00083               if ( BER_BVISNULL( &mapped_value ) || BER_BVISEMPTY( &mapped_value ) ) {
00084                      goto cleanup;
00085               }
00086 
00087        /*
00088         * else try to remap the attribute
00089         */
00090        } else {
00091               ldap_back_map( &mt->mt_rwmap.rwm_at,
00092                      &op->orc_ava->aa_desc->ad_cname,
00093                      &mapped_attr, BACKLDAP_MAP );
00094               if ( BER_BVISNULL( &mapped_attr ) || BER_BVISEMPTY( &mapped_attr ) ) {
00095                      goto cleanup;
00096               }
00097 
00098               if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
00099               {
00100                      dc.ctx = "compareAttrDN";
00101 
00102                      switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
00103                      {
00104                      case LDAP_UNWILLING_TO_PERFORM:
00105                             rc = 1;
00106                             goto cleanup;
00107 
00108                      default:
00109                             break;
00110                      }
00111               }
00112        }
00113 
00114 retry:;
00115        ctrls = op->o_ctrls;
00116        rc = meta_back_controls_add( op, rs, mc, candidate, &ctrls );
00117        if ( rc != LDAP_SUCCESS ) {
00118               send_ldap_result( op, rs );
00119               goto cleanup;
00120        }
00121 
00122        rs->sr_err = ldap_compare_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
00123                      mapped_attr.bv_val, &mapped_value,
00124                      ctrls, NULL, &msgid );
00125 
00126        rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
00127               mt->mt_timeout[ SLAP_OP_COMPARE ], ( LDAP_BACK_SENDRESULT | retrying ) );
00128        if ( rs->sr_err == LDAP_UNAVAILABLE && retrying ) {
00129               retrying &= ~LDAP_BACK_RETRYING;
00130               if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
00131                      /* if the identity changed, there might be need to re-authz */
00132                      (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00133                      goto retry;
00134               }
00135        }
00136 
00137 cleanup:;
00138        (void)mi->mi_ldap_extra->controls_free( op, rs, &ctrls );
00139 
00140        if ( mdn.bv_val != op->o_req_dn.bv_val ) {
00141               free( mdn.bv_val );
00142        }
00143 
00144        if ( op->orc_ava->aa_value.bv_val != mapped_value.bv_val ) {
00145               free( mapped_value.bv_val );
00146        }
00147 
00148        if ( mc ) {
00149               meta_back_release_conn( mi, mc );
00150        }
00151 
00152        return rs->sr_err;
00153 }
00154