]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/compare.c
0a8cf10e6bf7803ac194220ba7cfb4a2d3f74e02
[openldap] / servers / slapd / back-meta / compare.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2007 The OpenLDAP Foundation.
5  * Portions Copyright 2001-2003 Pierangelo Masarati.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by the Howard Chu for inclusion
19  * in OpenLDAP Software and subsequently enhanced by Pierangelo
20  * Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29
30 #include "slap.h"
31 #include "../back-ldap/back-ldap.h"
32 #include "back-meta.h"
33
34 int
35 meta_back_compare( Operation *op, SlapReply *rs )
36 {
37         metainfo_t      *mi = ( metainfo_t * )op->o_bd->be_private;
38         metatarget_t    *mt;
39         metaconn_t      *mc;
40         int             rc = 0;
41         int             candidate = -1;
42         struct berval   mdn = BER_BVNULL;
43         dncookie        dc;
44         struct berval   mapped_attr = op->orc_ava->aa_desc->ad_cname;
45         struct berval   mapped_value = op->orc_ava->aa_value;
46         int             msgid;
47         int             do_retry = 1;
48         LDAPControl     **ctrls = NULL;
49
50         mc = meta_back_getconn( op, rs, &candidate, LDAP_BACK_SENDERR );
51         if ( !mc || !meta_back_dobind( op, rs, mc, LDAP_BACK_SENDERR ) ) {
52                 return rs->sr_err;
53         }
54
55         assert( mc->mc_conns[ candidate ].msc_ld != NULL );
56
57         /*
58          * Rewrite the modify dn, if needed
59          */
60         mt = mi->mi_targets[ candidate ];
61         dc.target = mt;
62         dc.conn = op->o_conn;
63         dc.rs = rs;
64         dc.ctx = "compareDN";
65
66         switch ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
67         case LDAP_UNWILLING_TO_PERFORM:
68                 rc = 1;
69                 goto cleanup;
70
71         default:
72                 break;
73         }
74
75         /*
76          * if attr is objectClass, try to remap the value
77          */
78         if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass ) {
79                 ldap_back_map( &mt->mt_rwmap.rwm_oc,
80                                 &op->orc_ava->aa_value,
81                                 &mapped_value, BACKLDAP_MAP );
82
83                 if ( BER_BVISNULL( &mapped_value ) || BER_BVISEMPTY( &mapped_value ) ) {
84                         goto cleanup;
85                 }
86
87         /*
88          * else try to remap the attribute
89          */
90         } else {
91                 ldap_back_map( &mt->mt_rwmap.rwm_at,
92                         &op->orc_ava->aa_desc->ad_cname,
93                         &mapped_attr, BACKLDAP_MAP );
94                 if ( BER_BVISNULL( &mapped_attr ) || BER_BVISEMPTY( &mapped_attr ) ) {
95                         goto cleanup;
96                 }
97
98                 if ( op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName )
99                 {
100                         dc.ctx = "compareAttrDN";
101
102                         switch ( ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_value ) )
103                         {
104                         case LDAP_UNWILLING_TO_PERFORM:
105                                 rc = 1;
106                                 goto cleanup;
107
108                         default:
109                                 break;
110                         }
111                 }
112         }
113
114 retry:;
115         ctrls = op->o_ctrls;
116         rc = ldap_back_proxy_authz_ctrl( &mc->mc_conns[ candidate ].msc_bound_ndn,
117                 mt->mt_version, &mt->mt_idassert, op, rs, &ctrls );
118         if ( rc != LDAP_SUCCESS ) {
119                 send_ldap_result( op, rs );
120                 goto cleanup;
121         }
122
123         rs->sr_err = ldap_compare_ext( mc->mc_conns[ candidate ].msc_ld, mdn.bv_val,
124                         mapped_attr.bv_val, &mapped_value,
125                         ctrls, NULL, &msgid );
126
127         rs->sr_err = meta_back_op_result( mc, op, rs, candidate, msgid,
128                 mt->mt_timeout[ SLAP_OP_COMPARE ], LDAP_BACK_SENDRESULT );
129         if ( rs->sr_err == LDAP_UNAVAILABLE && do_retry ) {
130                 do_retry = 0;
131                 if ( meta_back_retry( op, rs, &mc, candidate, LDAP_BACK_SENDERR ) ) {
132                         /* if the identity changed, there might be need to re-authz */
133                         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
134                         goto retry;
135                 }
136         }
137
138 cleanup:;
139         (void)ldap_back_proxy_authz_ctrl_free( op, &ctrls );
140
141         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
142                 free( mdn.bv_val );
143         }
144
145         if ( op->orc_ava->aa_value.bv_val != mapped_value.bv_val ) {
146                 free( mapped_value.bv_val );
147         }
148
149         if ( mc ) {
150                 meta_back_release_conn( mi, mc );
151         }
152
153         return rs->sr_err;
154 }
155