]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/other.c
remove dnNormalize2
[openldap] / servers / slapd / back-sql / other.c
1 /*
2  *       Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved.
3  *
4  *       Redistribution and use in source and binary forms are permitted only
5  *       as authorized by the OpenLDAP Public License.  A copy of this
6  *       license is available at http://www.OpenLDAP.org/license.html or
7  *       in file LICENSE in the top-level directory of the distribution.
8  */
9
10 #include "portable.h"
11
12 #ifdef SLAPD_SQL
13
14 #include <stdio.h>
15 #include <sys/types.h>
16 #include "slap.h"
17 #include "back-sql.h"
18 #include "sql-wrap.h"
19 #include "entry-id.h"
20 #include "util.h"
21
22 int
23 backsql_compare( Operation *op, SlapReply *rs )
24 {
25         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
26         backsql_entryID         user_id;
27         SQLHDBC                 dbh;
28         Entry                   *e = NULL, user_entry;
29         Attribute               *a = NULL, *a_op = NULL;
30         backsql_srch_info       bsi;
31         int                     rc;
32         AttributeName           anlist[2];
33
34         user_entry.e_name.bv_val = NULL;
35         user_entry.e_name.bv_len = 0;
36         user_entry.e_nname.bv_val = NULL;
37         user_entry.e_nname.bv_len = 0;
38         user_entry.e_attrs = NULL;
39  
40         Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
41
42         rs->sr_err = backsql_get_db_conn( op, &dbh );
43         if (!dbh) {
44                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
45                         "could not get connection handle - exiting\n",
46                         0, 0, 0 );
47
48                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
49                         ? "SQL-backend error" : NULL;
50                 goto return_results;
51         }
52
53         rc = backsql_dn2id( bi, &user_id, dbh, &op->o_req_ndn );
54         if ( rc != LDAP_SUCCESS ) {
55                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
56                         "could not retrieve compare dn id - no such entry\n", 
57                         0, 0, 0 );
58                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
59                 goto return_results;
60         }
61
62         anlist[0].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
63         anlist[0].an_desc = op->oq_compare.rs_ava->aa_desc;
64         anlist[1].an_name.bv_val = NULL;
65
66         /*
67          * Try to get attr as dynamic operational
68          */
69         if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
70                 AttributeName   *an_old;
71                 Entry           *e_old;
72
73                 user_entry.e_attrs = NULL;
74                 user_entry.e_name = op->o_req_dn;
75                 user_entry.e_nname = op->o_req_ndn;
76
77                 an_old = rs->sr_attrs;
78                 e_old = rs->sr_entry;
79
80                 rs->sr_attrs = anlist;
81                 rs->sr_entry = &user_entry;
82                 rs->sr_err = backsql_operational( op, rs, 0, &a_op );
83                 rs->sr_attrs = an_old;
84                 rs->sr_entry = e_old;
85
86                 if ( rs->sr_err != LDAP_SUCCESS ) {
87                         goto return_results;
88                 }
89                 
90         }
91
92         /*
93          * attr was dynamic operational
94          */
95         if ( a_op != NULL ) {
96                 user_entry.e_attrs = a_op;
97                 e = &user_entry;
98
99         } else {
100                 backsql_init_search( &bsi, &op->o_req_ndn, LDAP_SCOPE_BASE, 
101                                 -1, -1, -1, NULL, dbh, op, anlist );
102                 e = backsql_id2entry( &bsi, &user_entry, &user_id );
103                 if ( e == NULL ) {
104                         Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
105                                 "error in backsql_id2entry() "
106                                 "- compare failed\n", 0, 0, 0 );
107                         rs->sr_err = LDAP_OTHER;
108                         goto return_results;
109                 }
110         }
111
112         if ( ! access_allowed( op, e, op->oq_compare.rs_ava->aa_desc, 
113                                 &op->oq_compare.rs_ava->aa_value,
114                                 ACL_COMPARE, NULL ) ) {
115                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
116                 goto return_results;
117         }
118
119         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
120         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
121                         a != NULL;
122                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
123         {
124                 rs->sr_err = LDAP_COMPARE_FALSE;
125                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
126                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
127                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
128                                         a->a_nvals,
129                                         &op->oq_compare.rs_ava->aa_value,
130                                         op->o_tmpmemctx ) == 0 )
131                 {
132                         rs->sr_err = LDAP_COMPARE_TRUE;
133                         break;
134                 }
135         }
136
137 return_results:;
138         send_ldap_result( op, rs );
139
140         if ( e != NULL ) {
141                 if ( e->e_name.bv_val != NULL ) {
142                         free( e->e_name.bv_val );
143                 }
144
145                 if ( e->e_nname.bv_val != NULL ) {
146                         free( e->e_nname.bv_val );
147                 }
148
149                 if ( e->e_attrs != NULL ) {
150                         attrs_free( e->e_attrs );
151                 }
152         }
153
154         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
155         switch ( rs->sr_err ) {
156         case LDAP_COMPARE_TRUE:
157         case LDAP_COMPARE_FALSE:
158                 return 0;
159
160         default:
161                 return 1;
162         }
163 }
164  
165 /*
166  * sets the supported operational attributes (if required)
167  */
168
169 int
170 backsql_operational(
171         Operation       *op,
172         SlapReply       *rs,
173         int             opattrs,
174         Attribute       **a )
175 {
176
177         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
178         SQLHDBC                 dbh = SQL_NULL_HDBC;
179         Attribute               **aa = a;
180         int                     rc = 0;
181
182         Debug( LDAP_DEBUG_TRACE, "==>backsql_operational(): entry '%s'\n",
183                         rs->sr_entry->e_nname.bv_val, 0, 0 );
184
185
186         if ( ( opattrs || ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) ) 
187                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL ) {
188                 
189                 rc = backsql_get_db_conn( op, &dbh );
190                 if ( rc != LDAP_SUCCESS ) {
191                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
192                                 "could not get connection handle - exiting\n", 
193                                 0, 0, 0 );
194                         return 1;
195                 }
196                 
197                 rc = backsql_has_children( bi, dbh, &rs->sr_entry->e_nname );
198
199                 switch( rc ) {
200                 case LDAP_COMPARE_TRUE:
201                 case LDAP_COMPARE_FALSE:
202                         *aa = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
203                         if ( *aa != NULL ) {
204                                 aa = &(*aa)->a_next;
205                         }
206                         rc = 0;
207                         break;
208
209                 default:
210                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
211                                 "has_children failed( %d)\n", rc, 0, 0 );
212                         rc = 1;
213                         break;
214                 }
215         }
216
217         Debug( LDAP_DEBUG_TRACE, "<==backsql_operational()\n", 0, 0, 0);
218
219         return rc;
220 }
221
222 #endif /* SLAPD_SQL */
223