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