]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/other.c
#ifdef -DSLAP_NVALUES
[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, user_entry;
29         Attribute               *a;
30         backsql_srch_info       bsi;
31         int                     rc;
32         AttributeName           anlist[2];
33  
34         Debug( LDAP_DEBUG_TRACE, "==>backsql_compare()\n", 0, 0, 0 );
35
36         rs->sr_err = backsql_get_db_conn( op, &dbh );
37         if (!dbh) {
38                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
39                         "could not get connection handle - exiting\n",
40                         0, 0, 0 );
41
42                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
43                         ? "SQL-backend error" : NULL;
44                 goto return_results;
45         }
46
47         rc = backsql_dn2id( bi, &user_id, dbh, &op->o_req_ndn );
48         if ( rc != LDAP_SUCCESS ) {
49                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
50                         "could not retrieve bind dn id - no such entry\n", 
51                         0, 0, 0 );
52                 rs->sr_err = LDAP_INVALID_CREDENTIALS;
53                 goto return_results;
54         }
55
56         anlist[0].an_name = op->oq_compare.rs_ava->aa_desc->ad_cname;
57         anlist[0].an_desc = op->oq_compare.rs_ava->aa_desc;
58         anlist[1].an_name.bv_val = NULL;
59         backsql_init_search( &bsi, bi, &op->o_req_ndn, LDAP_SCOPE_BASE, 
60                         -1, -1, -1, NULL, dbh, op->o_bd, op->o_conn, op,
61                         anlist);
62         e = backsql_id2entry( &bsi, &user_entry, &user_id );
63         if ( e == NULL ) {
64                 Debug( LDAP_DEBUG_TRACE, "backsql_compare(): "
65                         "error in backsql_id2entry() - auth failed\n",
66                         0, 0, 0 );
67                 rs->sr_err = LDAP_OTHER;
68                 goto return_results;
69         }
70
71         if ( ! access_allowed( op, e, op->oq_compare.rs_ava->aa_desc, 
72                                 &op->oq_compare.rs_ava->aa_value,
73                                 ACL_COMPARE, NULL ) ) {
74                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
75                 goto return_results;
76         }
77
78
79         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
80         for ( a = attrs_find( e->e_attrs, op->oq_compare.rs_ava->aa_desc );
81                         a != NULL;
82                         a = attrs_find( a->a_next, op->oq_compare.rs_ava->aa_desc ))
83         {
84                 rs->sr_err = LDAP_COMPARE_FALSE;
85                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
86                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
87                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
88                                         a->a_nvals, &op->oq_compare.rs_ava->aa_value ) == 0 )
89                 {
90                         rs->sr_err = LDAP_COMPARE_TRUE;
91                         break;
92                 }
93         }
94
95 return_results:;
96         send_ldap_result( op, rs );
97
98         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
99         switch ( rs->sr_err ) {
100         case LDAP_COMPARE_TRUE:
101         case LDAP_COMPARE_FALSE:
102                 return 0;
103
104         default:
105                 return 1;
106         }
107 }
108  
109 /*
110  * sets the supported operational attributes (if required)
111  */
112
113 int
114 backsql_operational(
115         Operation       *op,
116         SlapReply       *rs,
117         int             opattrs,
118         Attribute       **a )
119 {
120
121         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
122         SQLHDBC                 dbh = SQL_NULL_HDBC;
123         Attribute               **aa = a;
124         int                     rc = 0;
125
126         Debug( LDAP_DEBUG_TRACE, "==>backsql_operational(): entry '%s'\n",
127                         rs->sr_entry->e_nname.bv_val, 0, 0 );
128
129
130         if ( ( opattrs || ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) ) 
131                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL ) {
132                 
133                 rc = backsql_get_db_conn( op, &dbh );
134                 if ( rc != LDAP_SUCCESS ) {
135                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
136                                 "could not get connection handle - exiting\n", 
137                                 0, 0, 0 );
138                         return 1;
139                 }
140                 
141                 rc = backsql_has_children( bi, dbh, &rs->sr_entry->e_nname );
142
143                 switch( rc ) {
144                 case LDAP_COMPARE_TRUE:
145                 case LDAP_COMPARE_FALSE:
146                         *aa = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
147                         if ( *aa != NULL ) {
148                                 aa = &(*aa)->a_next;
149                         }
150                         rc = 0;
151                         break;
152
153                 default:
154                         Debug(LDAP_DEBUG_TRACE, 
155                                 "backsql_operational(): "
156                                 "has_children failed( %d)\n", 
157                                 rc, 0, 0 );
158                         rc = 1;
159                         break;
160                 }
161         }
162
163         return rc;
164 }
165
166 #endif /* SLAPD_SQL */
167