]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/other.c
more new API ... back-sql should be OK
[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 #ifdef SLAP_NVALUES
86                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
87                                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
88                                         SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
89                                         a->a_nvals, &op->oq_compare.rs_ava->aa_value ) == 0 )
90 #else
91                 if ( value_find( op->oq_compare.rs_ava->aa_desc, a->a_vals, &op->oq_compare.rs_ava->aa_value ) == 0 )
92 #endif
93                 {
94                         rs->sr_err = LDAP_COMPARE_TRUE;
95                         break;
96                 }
97         }
98
99 return_results:;
100         send_ldap_result( op, rs );
101
102         Debug(LDAP_DEBUG_TRACE,"<==backsql_compare()\n",0,0,0);
103         switch ( rs->sr_err ) {
104         case LDAP_COMPARE_TRUE:
105         case LDAP_COMPARE_FALSE:
106                 return 0;
107
108         default:
109                 return 1;
110         }
111 }
112  
113 /*
114  * sets the supported operational attributes (if required)
115  */
116
117 int
118 backsql_operational(
119         Operation       *op,
120         SlapReply       *rs,
121         int             opattrs,
122         Attribute       **a )
123 {
124
125         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
126         SQLHDBC                 dbh = SQL_NULL_HDBC;
127         Attribute               **aa = a;
128         int                     rc = 0;
129
130         Debug( LDAP_DEBUG_TRACE, "==>backsql_operational(): entry '%s'\n",
131                         rs->sr_entry->e_nname.bv_val, 0, 0 );
132
133
134         if ( ( opattrs || ad_inlist( slap_schema.si_ad_hasSubordinates, rs->sr_attrs ) ) 
135                         && attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_hasSubordinates ) == NULL ) {
136                 
137                 rc = backsql_get_db_conn( op, &dbh );
138                 if ( rc != LDAP_SUCCESS ) {
139                         Debug( LDAP_DEBUG_TRACE, "backsql_operational(): "
140                                 "could not get connection handle - exiting\n", 
141                                 0, 0, 0 );
142                         return 1;
143                 }
144                 
145                 rc = backsql_has_children( bi, dbh, &rs->sr_entry->e_nname );
146
147                 switch( rc ) {
148                 case LDAP_COMPARE_TRUE:
149                 case LDAP_COMPARE_FALSE:
150                         *aa = slap_operational_hasSubordinate( rc == LDAP_COMPARE_TRUE );
151                         if ( *aa != NULL ) {
152                                 aa = &(*aa)->a_next;
153                         }
154                         rc = 0;
155                         break;
156
157                 default:
158                         Debug(LDAP_DEBUG_TRACE, 
159                                 "backsql_operational(): "
160                                 "has_children failed( %d)\n", 
161                                 rc, 0, 0 );
162                         rc = 1;
163                         break;
164                 }
165         }
166
167         return rc;
168 }
169
170 #endif /* SLAPD_SQL */
171