]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/compare.c
Happy new year
[openldap] / servers / slapd / back-ldap / compare.c
1 /* compare.c - ldap backend compare function */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 1999-2003 Howard Chu.
7  * Portions Copyright 2000-2003 Pierangelo Masarati.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18 /* ACKNOWLEDGEMENTS:
19  * This work was initially developed by the Howard Chu for inclusion
20  * in OpenLDAP Software and subsequently enhanced by Pierangelo
21  * Masarati.
22  */
23
24 #include "portable.h"
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/socket.h>
30
31 #include "slap.h"
32 #include "back-ldap.h"
33
34 int
35 ldap_back_compare(
36     Operation   *op,
37     SlapReply   *rs )
38 {
39         struct ldapinfo *li = (struct ldapinfo *) op->o_bd->be_private;
40         struct ldapconn *lc;
41         struct berval mapped_at = { 0, NULL }, mapped_val = { 0, NULL };
42         struct berval mdn = { 0, NULL };
43         ber_int_t msgid;
44         int freeval = 0;
45         dncookie dc;
46 #ifdef LDAP_BACK_PROXY_AUTHZ 
47         LDAPControl **ctrls = NULL;
48         int rc = LDAP_SUCCESS;
49 #endif /* LDAP_BACK_PROXY_AUTHZ */
50
51         lc = ldap_back_getconn(op, rs);
52         if (!lc || !ldap_back_dobind( lc, op, rs ) ) {
53                 return( -1 );
54         }
55
56         /*
57          * Rewrite the compare dn, if needed
58          */
59         dc.rwmap = &li->rwmap;
60 #ifdef ENABLE_REWRITE
61         dc.conn = op->o_conn;
62         dc.rs = rs;
63         dc.ctx = "compareDn";
64 #else
65         dc.tofrom = 1;
66         dc.normalized = 0;
67 #endif
68         if ( ldap_back_dn_massage( &dc, &op->o_req_dn, &mdn ) ) {
69                 send_ldap_result( op, rs );
70                 return -1;
71         }
72
73         if ( op->orc_ava->aa_desc == slap_schema.si_ad_objectClass
74                 || op->orc_ava->aa_desc == slap_schema.si_ad_structuralObjectClass ) {
75                 ldap_back_map(&li->rwmap.rwm_oc, &op->orc_ava->aa_value,
76                                 &mapped_val, BACKLDAP_MAP);
77                 if (mapped_val.bv_val == NULL || mapped_val.bv_val[0] == '\0') {
78                         return( -1 );
79                 }
80                 mapped_at = op->orc_ava->aa_desc->ad_cname;
81         } else {
82                 ldap_back_map(&li->rwmap.rwm_at,
83                                 &op->orc_ava->aa_desc->ad_cname, &mapped_at, 
84                                 BACKLDAP_MAP);
85                 if (mapped_at.bv_val == NULL || mapped_at.bv_val[0] == '\0') {
86                         return( -1 );
87                 }
88                 if (op->orc_ava->aa_desc->ad_type->sat_syntax == slap_schema.si_syn_distinguishedName ) {
89 #ifdef ENABLE_REWRITE
90                         dc.ctx = "compareAttrDN";
91 #endif
92                         ldap_back_dn_massage( &dc, &op->orc_ava->aa_value, &mapped_val );
93                         if (mapped_val.bv_val == NULL || mapped_val.bv_val[0] == '\0') {
94                                 mapped_val = op->orc_ava->aa_value;
95                         } else if (mapped_val.bv_val != op->orc_ava->aa_value.bv_val) {
96                                 freeval = 1;
97                         }
98                 } else {
99                         mapped_val = op->orc_ava->aa_value;
100                 }
101         }
102
103 #ifdef LDAP_BACK_PROXY_AUTHZ
104         rc = ldap_back_proxy_authz_ctrl( lc, op, rs, &ctrls );
105         if ( rc != LDAP_SUCCESS ) {
106                 goto cleanup;
107         }
108 #endif /* LDAP_BACK_PROXY_AUTHZ */
109
110         rs->sr_err = ldap_compare_ext( lc->ld, mdn.bv_val,
111                         mapped_at.bv_val, &mapped_val, 
112 #ifdef LDAP_BACK_PROXY_AUTHZ
113                         ctrls,
114 #else /* ! LDAP_BACK_PROXY_AUTHZ */
115                         op->o_ctrls,
116 #endif /* ! LDAP_BACK_PROXY_AUTHZ */
117                         NULL, &msgid );
118
119 #ifdef LDAP_BACK_PROXY_AUTHZ
120 cleanup:
121         if ( ctrls && ctrls != op->o_ctrls ) {
122                 free( ctrls[ 0 ] );
123                 free( ctrls );
124         }
125 #endif /* LDAP_BACK_PROXY_AUTHZ */
126         
127         if ( mdn.bv_val != op->o_req_dn.bv_val ) {
128                 free( mdn.bv_val );
129         }
130         if ( freeval ) {
131                 free( mapped_val.bv_val );
132         }
133
134 #ifdef LDAP_BACK_PROXY_AUTHZ
135         if ( rc != LDAP_SUCCESS ) {
136                 send_ldap_result( op, rs );
137                 return -1;
138         }
139 #endif /* LDAP_BACK_PROXY_AUTHZ */
140         return( ldap_back_op_result( lc, op, rs, msgid, 1 ) );
141 }