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