]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
79d8dec530d590a7e78b5331f2e2671fd4c58e68
[openldap] / servers / slapd / back-sql / init.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 "ldap_pvt.h"
18 #include "back-sql.h"
19 #include "sql-wrap.h"
20 #include "schema-map.h"
21 #include "util.h"
22
23 #ifdef SLAPD_SQL_DYNAMIC
24
25 int
26 backsql_LTX_init_module(
27         int             argc, 
28         char            *argv[] )
29 {
30         BackendInfo bi;
31
32         memset( &bi, '\0', sizeof( bi ) );
33         bi.bi_type = "sql";
34         bi.bi_init = backbacksql_initialize;
35
36         backend_add( &bi );
37         return 0;
38 }
39
40 #endif /* SLAPD_SHELL_DYNAMIC */
41
42 int
43 sql_back_initialize(
44         BackendInfo     *bi )
45
46         Debug( LDAP_DEBUG_TRACE,"==>backsql_initialize()\n", 0, 0, 0 );
47         
48         bi->bi_open = 0;
49         bi->bi_config = 0;
50         bi->bi_close = 0;
51         bi->bi_destroy = 0;
52
53         bi->bi_db_init = backsql_db_init;
54         bi->bi_db_config = backsql_db_config;
55         bi->bi_db_open = backsql_db_open;
56         bi->bi_db_close = backsql_db_close;
57         bi->bi_db_destroy = backsql_db_destroy;
58
59 #ifdef BACKSQL_ALL_DONE
60         bi->bi_op_abandon = backsql_abandon;
61         bi->bi_op_compare = backsql_compare;
62 #else
63         bi->bi_op_abandon = 0;
64         bi->bi_op_compare = 0;
65 #endif
66         bi->bi_op_bind = backsql_bind;
67         bi->bi_op_unbind = backsql_unbind;
68         bi->bi_op_search = backsql_search;
69         bi->bi_op_modify = backsql_modify;
70         bi->bi_op_modrdn = backsql_modrdn;
71         bi->bi_op_add = backsql_add;
72         bi->bi_op_delete = backsql_delete;
73         
74         bi->bi_acl_group = 0;
75         bi->bi_acl_attribute = 0;
76         bi->bi_chk_referrals = 0;
77  
78         bi->bi_connection_init = 0;
79         bi->bi_connection_destroy = backsql_connection_destroy;
80         
81         Debug( LDAP_DEBUG_TRACE,"<==backsql_initialize()\n", 0, 0, 0 );
82         return 0;
83 }
84
85
86 int
87 backsql_destroy( 
88         BackendInfo     *bi )
89 {
90         Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
91         Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
92         return 0;
93 }
94
95 int
96 backsql_db_init(
97         BackendDB       *bd )
98 {
99         backsql_info *si;
100  
101         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
102         si = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
103         ldap_pvt_thread_mutex_init( &si->dbconn_mutex );
104         ldap_pvt_thread_mutex_init( &si->schema_mutex );
105         backsql_init_db_env( si );
106         si->has_ldapinfo_dn_ru = -1;
107
108         bd->be_private = si;
109         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
110         return 0;
111 }
112
113 int
114 backsql_db_destroy(
115         BackendDB       *bd )
116 {
117         backsql_info *si = (backsql_info*)bd->be_private;
118  
119         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
120         ldap_pvt_thread_mutex_lock( &si->dbconn_mutex );
121         backsql_free_db_env( si );
122         ldap_pvt_thread_mutex_unlock( &si->dbconn_mutex );
123         ldap_pvt_thread_mutex_lock( &si->schema_mutex );
124         backsql_destroy_schema_map( si );
125         ldap_pvt_thread_mutex_unlock( &si->schema_mutex );
126         ldap_pvt_thread_mutex_destroy( &si->schema_mutex );
127         ldap_pvt_thread_mutex_destroy( &si->dbconn_mutex );
128         free( si->dbname );
129         free( si->dbuser );
130         if ( si->dbpasswd ) {
131                 free( si->dbpasswd );
132         }
133         if ( si->dbhost ) {
134                 free( si->dbhost );
135         }
136         if ( si->upper_func ) {
137                 free( si->upper_func );
138         }
139         
140         free( si->subtree_cond );
141         free( si->oc_query );
142         free( si->at_query );
143         free( si->insentry_query );
144         free( si->delentry_query );
145         free( si );
146         
147         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
148         return 0;
149 }
150
151 int
152 backsql_db_open(
153         BackendDB       *bd )
154 {
155         backsql_info    *si = (backsql_info*)bd->be_private;
156         Connection      tmp;
157         SQLHDBC         dbh;
158         int             idq_len;
159         struct berval   bv;
160
161         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
162                 "testing RDBMS connection\n", 0, 0, 0 );
163         if ( si->dbname == NULL ) {
164                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
165                         "datasource name not specified "
166                         "(use dbname directive in slapd.conf)\n", 0, 0, 0 );
167                 return 1;
168         }
169         
170         if ( si->dbuser == NULL ) {
171                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
172                         "user name not specified "
173                         "(use dbuser directive in slapd.conf)\n", 0, 0, 0 );
174                 return 1;
175         }
176         
177         if ( si->subtree_cond == NULL ) {
178                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
179                         "subtree search SQL condition not specified "
180                         "(use subtree_cond directive in slapd.conf)\n", 
181                         0, 0, 0);
182                 if ( si->upper_func ) {
183                         struct berval   bv = { 0, NULL };
184                         int             len = 0;
185
186                         backsql_strcat( &bv, &len, si->upper_func,
187                                         backsql_def_upper_subtree_cond, NULL );
188                         si->subtree_cond = bv.bv_val;
189                 } else {
190                         si->subtree_cond = ch_strdup( backsql_def_subtree_cond );
191                 }
192                         
193                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
194                         "setting '%s' as default\n",
195                         si->subtree_cond, 0, 0 );
196         }
197
198         if ( si->oc_query == NULL ) {
199                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
200                         "objectclass mapping SQL statement not specified "
201                         "(use oc_query directive in slapd.conf)\n", 0, 0, 0 );
202                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
203                         "setting '%s' by default\n", 
204                         backsql_def_oc_query, 0, 0 );
205                 si->oc_query = ch_strdup( backsql_def_oc_query );
206         }
207         
208         if ( si->at_query == NULL ) {
209                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
210                         "attribute mapping SQL statement not specified "
211                         "(use at_query directive in slapd.conf)\n",
212                         0, 0, 0 );
213                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
214                         "setting '%s' by default\n",
215                         backsql_def_at_query, 0, 0 );
216                 si->at_query = ch_strdup( backsql_def_at_query );
217         }
218         
219         if ( si->insentry_query == NULL ) {
220                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
221                         "entry insertion SQL statement not specified "
222                         "(use insentry_query directive in slapd.conf)\n",
223                         0, 0, 0 );
224                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
225                         "setting '%s' by default\n",
226                         backsql_def_insentry_query, 0, 0 );
227                 si->insentry_query = ch_strdup( backsql_def_insentry_query );
228         }
229         
230         if ( si->delentry_query == NULL ) {
231                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
232                         "entry deletion SQL statement not specified "
233                         "(use delentry_query directive in slapd.conf)\n",
234                         0, 0, 0 );
235                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
236                         "setting '%s' by default\n",
237                         backsql_def_delentry_query, 0, 0 );
238                 si->delentry_query = ch_strdup( backsql_def_delentry_query );
239         }
240         
241         tmp.c_connid =- 1;
242         if ( backsql_get_db_conn( bd, &tmp, &dbh ) != LDAP_SUCCESS ) {
243                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
244                         "connection failed, exiting\n", 0, 0, 0 );
245                 return 1;
246         }
247
248         si->id_query = NULL;
249         idq_len = 0;
250
251         bv.bv_val = NULL;
252         bv.bv_len = 0;
253         if ( si->upper_func == NULL ) {
254                 backsql_strcat( &bv, &idq_len, backsql_id_query, 
255                                 "dn=?", NULL );
256         } else {
257                 if ( si->has_ldapinfo_dn_ru ) {
258                         backsql_strcat( &bv, &idq_len, backsql_id_query,
259                                         "dn_ru=?", NULL );
260                 } else {
261                         if ( si->isTimesTen ) {
262                                 backsql_strcat( &bv, &idq_len, 
263                                                 backsql_id_query,
264                                                 si->upper_func, "(dn)=?",
265                                                 NULL );
266                         } else {
267                                 backsql_strcat( &bv, &idq_len, 
268                                                 backsql_id_query,
269                                                 si->upper_func, "(dn)=",
270                                                 si->upper_func, "(?)", NULL );
271                         }
272                 }
273         }
274         si->id_query = bv.bv_val;
275  
276         backsql_free_db_conn( bd, &tmp );
277         if ( !si->schema_loaded ) {
278                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
279                         "test failed, schema map not loaded - exiting\n",
280                         0, 0, 0 );
281                 return 1;
282         }
283         
284         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
285                 "test succeeded, schema map loaded\n", 0, 0, 0 );
286         return 0;
287 }
288
289 int
290 backsql_db_close(
291         BackendDB       *bd )
292 {
293         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
294         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
295         return 0;
296 }
297
298 int
299 backsql_connection_destroy(
300         BackendDB       *be,
301         Connection      *conn )
302 {
303         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
304         backsql_free_db_conn( be, conn );
305         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
306         return 0;
307 }
308
309 #endif /* SLAPD_SQL */
310