]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
501dedaee65c57cd2a52fce62f78552bf93c104c
[openldap] / servers / slapd / back-sql / init.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include "ac/string.h"
26
27 #include "slap.h"
28 #include "proto-sql.h"
29
30 int
31 sql_back_initialize(
32         BackendInfo     *bi )
33
34         static char *controls[] = {
35 #if 0 /* needs improvements */
36 #ifdef LDAP_CONTROL_NOOP
37                 LDAP_CONTROL_NOOP,
38 #endif /* LDAP_CONTROL_NOOP */
39 #endif
40 #ifdef LDAP_CONTROL_VALUESRETURNFILTER
41                 LDAP_CONTROL_VALUESRETURNFILTER,
42 #endif /* LDAP_CONTROL_VALUESRETURNFILTER */
43                 NULL
44         };
45
46         bi->bi_controls = controls;
47
48         Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 0 );
49         
50         bi->bi_db_init = backsql_db_init;
51         bi->bi_db_config = backsql_db_config;
52         bi->bi_db_open = backsql_db_open;
53         bi->bi_db_close = backsql_db_close;
54         bi->bi_db_destroy = backsql_db_destroy;
55
56         bi->bi_op_abandon = 0;
57         bi->bi_op_compare = backsql_compare;
58         bi->bi_op_bind = backsql_bind;
59         bi->bi_op_unbind = 0;
60         bi->bi_op_search = backsql_search;
61         bi->bi_op_modify = backsql_modify;
62         bi->bi_op_modrdn = backsql_modrdn;
63         bi->bi_op_add = backsql_add;
64         bi->bi_op_delete = backsql_delete;
65         
66         bi->bi_chk_referrals = 0;
67         bi->bi_operational = backsql_operational;
68         bi->bi_entry_get_rw = backsql_entry_get;
69  
70         bi->bi_connection_init = 0;
71         bi->bi_connection_destroy = backsql_connection_destroy;
72
73         Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
74         return 0;
75 }
76
77 int
78 backsql_destroy( 
79         BackendInfo     *bi )
80 {
81         Debug( LDAP_DEBUG_TRACE, "==>backsql_destroy()\n", 0, 0, 0 );
82         Debug( LDAP_DEBUG_TRACE, "<==backsql_destroy()\n", 0, 0, 0 );
83         return 0;
84 }
85
86 int
87 backsql_db_init(
88         BackendDB       *bd )
89 {
90         backsql_info    *bi;
91  
92         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
93         bi = (backsql_info *)ch_calloc( 1, sizeof( backsql_info ) );
94         memset( bi, '\0', sizeof( backsql_info ) );
95         ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
96         ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
97         backsql_init_db_env( bi );
98
99         bd->be_private = bi;
100         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_init()\n", 0, 0, 0 );
101         return 0;
102 }
103
104 int
105 backsql_db_destroy(
106         BackendDB       *bd )
107 {
108         backsql_info    *bi = (backsql_info*)bd->be_private;
109  
110         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
111         ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
112         backsql_free_db_env( bi );
113         ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
114         ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
115         ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
116         backsql_destroy_schema_map( bi );
117         ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
118         ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
119         free( bi->sql_dbname );
120         free( bi->sql_dbuser );
121         if ( bi->sql_dbpasswd ) {
122                 free( bi->sql_dbpasswd );
123         }
124         if ( bi->sql_dbhost ) {
125                 free( bi->sql_dbhost );
126         }
127         if ( bi->sql_upper_func.bv_val ) {
128                 free( bi->sql_upper_func.bv_val );
129                 free( bi->sql_upper_func_open.bv_val );
130                 free( bi->sql_upper_func_close.bv_val );
131         }
132         
133         free( bi->sql_subtree_cond.bv_val );
134         free( bi->sql_oc_query );
135         free( bi->sql_at_query );
136         free( bi->sql_insentry_query );
137         free( bi->sql_delentry_query );
138         free( bi->sql_delobjclasses_query );
139         free( bi->sql_delreferrals_query );
140
141         if ( bi->sql_baseObject ) {
142                 entry_free( bi->sql_baseObject );
143         }
144         
145         free( bi );
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    *bi = (backsql_info*)bd->be_private;
156         SQLHDBC         dbh;
157         ber_len_t       idq_len;
158         struct berbuf   bb = BB_NULL;
159
160         char            opbuf[ OPERATION_BUFFER_SIZE ];
161         Operation*      op = (Operation *)opbuf;
162         
163         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
164                 "testing RDBMS connection\n", 0, 0, 0 );
165         if ( bi->sql_dbname == NULL ) {
166                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
167                         "datasource name not specified "
168                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
169                 return 1;
170         }
171
172         if ( bi->sql_concat_func == NULL ) {
173                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
174                         "concat func not specified (use \"concat_pattern\" "
175                         "directive in slapd.conf)\n", 0, 0, 0 );
176
177                 if ( backsql_split_pattern( backsql_def_concat_func, 
178                                 &bi->sql_concat_func, 2 ) ) {
179                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
180                                 "unable to parse pattern \"%s\"",
181                                 backsql_def_concat_func, 0, 0 );
182                         return 1;
183                 }
184         }
185
186         /*
187          * Prepare cast string as required
188          */
189         if ( bi->sql_upper_func.bv_val ) {
190                 char buf[1024];
191
192                 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
193                         snprintf( buf, sizeof( buf ), 
194                                 "%s(cast (" /* ? as varchar(%d))) */ , 
195                                 bi->sql_upper_func.bv_val );
196                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
197
198                         snprintf( buf, sizeof( buf ),
199                                 /* (cast(? */ " as varchar(%d)))",
200                                 BACKSQL_MAX_DN_LEN );
201                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
202
203                 } else {
204                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
205                                         bi->sql_upper_func.bv_val );
206                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
207
208                         ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
209                 }
210         }
211
212         /* normalize filter values only if necessary */
213         bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
214         assert( bi->sql_caseIgnoreMatch );
215
216         bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
217         assert( bi->sql_telephoneNumberMatch );
218
219         if ( bi->sql_dbuser == NULL ) {
220                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
221                         "user name not specified "
222                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
223                 return 1;
224         }
225         
226         if ( bi->sql_subtree_cond.bv_val == NULL ) {
227                 /*
228                  * Prepare concat function for subtree search condition
229                  */
230                 struct berval   concat;
231                 struct berval   values[] = {
232                         BER_BVC( "'%'" ),
233                         BER_BVC( "?" ),
234                         BER_BVNULL
235                 };
236                 struct berbuf   bb = BB_NULL;
237
238                 if ( backsql_prepare_pattern( bi->sql_concat_func, values, 
239                                 &concat ) ) {
240                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
241                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
242                         return 1;
243                 }
244                         
245                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
246                         "subtree search SQL condition not specified "
247                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
248                         0, 0, 0);
249
250                 if ( bi->sql_upper_func.bv_val ) {
251
252                         /*
253                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
254                          */
255
256                         backsql_strfcat( &bb, "blbbb",
257                                         &bi->sql_upper_func,
258                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
259                                                 "(ldap_entries.dn) LIKE ",
260                                         &bi->sql_upper_func_open,
261                                         &concat,
262                                         &bi->sql_upper_func_close );
263
264                 } else {
265
266                         /*
267                          * ldap_entries.dn LIKE CONCAT('%',?)
268                          */
269
270                         backsql_strfcat( &bb, "lb",
271                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
272                                                 "ldap_entries.dn LIKE ",
273                                         &concat );
274                 }
275
276                 bi->sql_subtree_cond = bb.bb_val;
277                         
278                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
279                         "setting \"%s\" as default\n",
280                         bi->sql_subtree_cond.bv_val, 0, 0 );
281         }
282
283         if ( bi->sql_children_cond.bv_val == NULL ) {
284                 struct berbuf   bb = BB_NULL;
285
286                 if ( bi->sql_upper_func.bv_val ) {
287
288                         /*
289                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
290                          */
291
292                         backsql_strfcat( &bb, "blbl",
293                                         &bi->sql_upper_func,
294                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
295                                                 "(ldap_entries.dn)=",
296                                         &bi->sql_upper_func,
297                                         (ber_len_t)STRLENOF( "(?)" ), "(?)" );
298
299                 } else {
300
301                         /*
302                          * ldap_entries.dn LIKE CONCAT('%,',?)
303                          */
304
305                         backsql_strfcat( &bb, "l",
306                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
307                                                 "ldap_entries.dn=?");
308                 }
309
310                 bi->sql_children_cond = bb.bb_val;
311                         
312                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
313                         "setting \"%s\" as default\n",
314                         bi->sql_children_cond.bv_val, 0, 0 );
315         }
316
317         if ( bi->sql_oc_query == NULL ) {
318                 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
319                         bi->sql_oc_query =
320                                 ch_strdup( backsql_def_needs_select_oc_query );
321
322                 } else {
323                         bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
324                 }
325
326                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
327                         "objectclass mapping SQL statement not specified "
328                         "(use \"oc_query\" directive in slapd.conf)\n", 
329                         0, 0, 0 );
330                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
331                         "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
332         }
333         
334         if ( bi->sql_at_query == NULL ) {
335                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
336                         "attribute mapping SQL statement not specified "
337                         "(use \"at_query\" directive in slapd.conf)\n",
338                         0, 0, 0 );
339                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
340                         "setting \"%s\" by default\n",
341                         backsql_def_at_query, 0, 0 );
342                 bi->sql_at_query = ch_strdup( backsql_def_at_query );
343         }
344         
345         if ( bi->sql_insentry_query == NULL ) {
346                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
347                         "entry insertion SQL statement not specified "
348                         "(use \"insentry_query\" directive in slapd.conf)\n",
349                         0, 0, 0 );
350                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
351                         "setting \"%s\" by default\n",
352                         backsql_def_insentry_query, 0, 0 );
353                 bi->sql_insentry_query = ch_strdup( backsql_def_insentry_query );
354         }
355         
356         if ( bi->sql_delentry_query == NULL ) {
357                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
358                         "entry deletion SQL statement not specified "
359                         "(use \"delentry_query\" directive in slapd.conf)\n",
360                         0, 0, 0 );
361                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
362                         "setting \"%s\" by default\n",
363                         backsql_def_delentry_query, 0, 0 );
364                 bi->sql_delentry_query = ch_strdup( backsql_def_delentry_query );
365         }
366
367         if ( bi->sql_delobjclasses_query == NULL ) {
368                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
369                         "objclasses deletion SQL statement not specified "
370                         "(use \"delobjclasses_query\" directive in slapd.conf)\n",
371                         0, 0, 0 );
372                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
373                         "setting \"%s\" by default\n",
374                         backsql_def_delobjclasses_query, 0, 0 );
375                 bi->sql_delobjclasses_query = ch_strdup( backsql_def_delobjclasses_query );
376         }
377
378         if ( bi->sql_delreferrals_query == NULL ) {
379                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
380                         "referrals deletion SQL statement not specified "
381                         "(use \"delreferrals_query\" directive in slapd.conf)\n",
382                         0, 0, 0 );
383                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
384                         "setting \"%s\" by default\n",
385                         backsql_def_delreferrals_query, 0, 0 );
386                 bi->sql_delreferrals_query = ch_strdup( backsql_def_delreferrals_query );
387         }
388
389         op->o_hdr = (Opheader *)&op[ 1 ];
390         op->o_connid = (unsigned long)(-1);
391         op->o_bd = bd;
392         if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
393                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
394                         "connection failed, exiting\n", 0, 0, 0 );
395                 return 1;
396         }
397
398         /*
399          * Prepare ID selection query
400          */
401         bi->sql_id_query = NULL;
402         idq_len = 0;
403
404         if ( bi->sql_upper_func.bv_val == NULL ) {
405                 backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
406
407         } else {
408                 if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
409                         backsql_strcat( &bb, backsql_id_query,
410                                         "dn_ru=?", NULL );
411                 } else {
412                         if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
413                                 backsql_strfcat( &bb, "sbl",
414                                                 backsql_id_query,
415                                                 &bi->sql_upper_func, 
416                                                 (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
417                         } else {
418                                 backsql_strfcat( &bb, "sblbcb",
419                                                 backsql_id_query,
420                                                 &bi->sql_upper_func, 
421                                                 (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
422                                                 &bi->sql_upper_func_open, 
423                                                 '?', 
424                                                 &bi->sql_upper_func_close );
425                         }
426                 }
427         }
428         bi->sql_id_query = bb.bb_val.bv_val;
429
430         /*
431          * Prepare children ID selection query
432          */
433         bi->sql_has_children_query = NULL;
434
435         bb.bb_val.bv_val = NULL;
436         bb.bb_val.bv_len = 0;
437         bb.bb_len = 0;
438         backsql_strfcat( &bb, "sb",
439                         "SELECT COUNT(distinct subordinates.id) FROM ldap_entries,ldap_entries subordinates WHERE subordinates.parent=ldap_entries.id AND ",
440
441                         &bi->sql_children_cond );
442         bi->sql_has_children_query = bb.bb_val.bv_val;
443  
444         backsql_free_db_conn( op );
445         if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
446                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
447                         "test failed, schema map not loaded - exiting\n",
448                         0, 0, 0 );
449                 return 1;
450         }
451         
452         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
453                 "test succeeded, schema map loaded\n", 0, 0, 0 );
454         return 0;
455 }
456
457 int
458 backsql_db_close(
459         BackendDB       *bd )
460 {
461         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
462         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
463         return 0;
464 }
465
466 int
467 backsql_connection_destroy( Backend *bd, Connection *c )
468 {
469         char            opbuf[ OPERATION_BUFFER_SIZE ];
470         Operation*      op = (Operation *)opbuf;
471
472         op->o_hdr = (Opheader *)&op[ 1 ];
473         op->o_connid = c->c_connid;
474         op->o_bd = bd;
475
476         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
477         backsql_free_db_conn( op );
478         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
479
480         return 0;
481 }
482
483 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
484
485 /* conditionally define the init_module() function */
486 SLAP_BACKEND_INIT_MODULE( sql )
487
488 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
489