]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
f5502eb237ee745da4c10b1e5081c672f371e4e4
[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-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22
23 #include "portable.h"
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
32 int
33 sql_back_initialize(
34         BackendInfo     *bi )
35
36         static char *controls[] = {
37                 LDAP_CONTROL_ASSERT,
38                 LDAP_CONTROL_MANAGEDSAIT,
39 #if 0 /* needs improvements */
40                 LDAP_CONTROL_NOOP,
41 #endif
42 #ifdef SLAP_CONTROL_X_TREE_DELETE
43                 SLAP_CONTROL_X_TREE_DELETE,
44 #endif /* SLAP_CONTROL_X_TREE_DELETE */
45                 NULL
46         };
47
48         bi->bi_controls = controls;
49
50         bi->bi_flags |=
51 #if 0
52                 SLAP_BFLAG_INCREMENT |
53 #endif
54                 SLAP_BFLAG_REFERRALS;
55
56         Debug( LDAP_DEBUG_TRACE,"==>sql_back_initialize()\n", 0, 0, 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         bi->bi_entry_get_rw = backsql_entry_get;
77         bi->bi_entry_release_rw = backsql_entry_release;
78  
79         bi->bi_connection_init = 0;
80         bi->bi_connection_destroy = backsql_connection_destroy;
81
82         Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
83         return 0;
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    *bi;
100  
101         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_init()\n", 0, 0, 0 );
102         bi = (backsql_info *)ch_malloc( sizeof( backsql_info ) );
103         memset( bi, '\0', sizeof( backsql_info ) );
104         ldap_pvt_thread_mutex_init( &bi->sql_dbconn_mutex );
105         ldap_pvt_thread_mutex_init( &bi->sql_schema_mutex );
106         backsql_init_db_env( bi );
107
108         bd->be_private = bi;
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    *bi = (backsql_info*)bd->be_private;
118  
119         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_destroy()\n", 0, 0, 0 );
120         ldap_pvt_thread_mutex_lock( &bi->sql_dbconn_mutex );
121         backsql_free_db_env( bi );
122         ldap_pvt_thread_mutex_unlock( &bi->sql_dbconn_mutex );
123         ldap_pvt_thread_mutex_destroy( &bi->sql_dbconn_mutex );
124         ldap_pvt_thread_mutex_lock( &bi->sql_schema_mutex );
125         backsql_destroy_schema_map( bi );
126         ldap_pvt_thread_mutex_unlock( &bi->sql_schema_mutex );
127         ldap_pvt_thread_mutex_destroy( &bi->sql_schema_mutex );
128         free( bi->sql_dbname );
129         free( bi->sql_dbuser );
130         if ( bi->sql_dbpasswd ) {
131                 free( bi->sql_dbpasswd );
132         }
133         if ( bi->sql_dbhost ) {
134                 free( bi->sql_dbhost );
135         }
136         if ( bi->sql_upper_func.bv_val ) {
137                 free( bi->sql_upper_func.bv_val );
138                 free( bi->sql_upper_func_open.bv_val );
139                 free( bi->sql_upper_func_close.bv_val );
140         }
141         
142         free( bi->sql_subtree_cond.bv_val );
143         free( bi->sql_oc_query );
144         free( bi->sql_at_query );
145         free( bi->sql_insentry_stmt );
146         free( bi->sql_delentry_stmt );
147         free( bi->sql_renentry_stmt );
148         free( bi->sql_delobjclasses_stmt );
149
150         free( bi->sql_aliasing.bv_val );
151         free( bi->sql_aliasing_quote.bv_val );
152
153         if ( bi->sql_anlist ) {
154                 int     i;
155
156                 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[i].an_name ); i++ )
157                 {
158                         ch_free( bi->sql_anlist[i].an_name.bv_val );
159                 }
160                 ch_free( bi->sql_anlist );
161         }
162
163         if ( bi->sql_baseObject ) {
164                 entry_free( bi->sql_baseObject );
165         }
166         
167         free( bi );
168         
169         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
170         return 0;
171 }
172
173 int
174 backsql_db_open(
175         BackendDB       *bd )
176 {
177         backsql_info    *bi = (backsql_info*)bd->be_private;
178         SQLHDBC         dbh = SQL_NULL_HDBC;
179         struct berbuf   bb = BB_NULL;
180
181         char            opbuf[ OPERATION_BUFFER_SIZE ];
182         Operation*      op = (Operation *)opbuf;
183         
184         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
185                 "testing RDBMS connection\n", 0, 0, 0 );
186         if ( bi->sql_dbname == NULL ) {
187                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
188                         "datasource name not specified "
189                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
190                 return 1;
191         }
192
193         if ( bi->sql_concat_func == NULL ) {
194                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
195                         "concat func not specified (use \"concat_pattern\" "
196                         "directive in slapd.conf)\n", 0, 0, 0 );
197
198                 if ( backsql_split_pattern( backsql_def_concat_func, 
199                                 &bi->sql_concat_func, 2 ) ) {
200                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
201                                 "unable to parse pattern \"%s\"",
202                                 backsql_def_concat_func, 0, 0 );
203                         return 1;
204                 }
205         }
206
207         /*
208          * see back-sql.h for default values
209          */
210         if ( BER_BVISNULL( &bi->sql_aliasing ) ) {
211                 ber_str2bv( BACKSQL_ALIASING,
212                         STRLENOF( BACKSQL_ALIASING ),
213                         1, &bi->sql_aliasing );
214         }
215
216         if ( BER_BVISNULL( &bi->sql_aliasing_quote ) ) {
217                 ber_str2bv( BACKSQL_ALIASING_QUOTE,
218                         STRLENOF( BACKSQL_ALIASING_QUOTE ),
219                         1, &bi->sql_aliasing_quote );
220         }
221
222         /*
223          * Prepare cast string as required
224          */
225         if ( bi->sql_upper_func.bv_val ) {
226                 char buf[1024];
227
228                 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
229                         snprintf( buf, sizeof( buf ), 
230                                 "%s(cast (" /* ? as varchar(%d))) */ , 
231                                 bi->sql_upper_func.bv_val );
232                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
233
234                         snprintf( buf, sizeof( buf ),
235                                 /* (cast(? */ " as varchar(%d)))",
236                                 BACKSQL_MAX_DN_LEN );
237                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
238
239                 } else {
240                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
241                                         bi->sql_upper_func.bv_val );
242                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
243
244                         ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
245                 }
246         }
247
248         /* normalize filter values only if necessary */
249         bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
250         assert( bi->sql_caseIgnoreMatch != NULL );
251
252         bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
253         assert( bi->sql_telephoneNumberMatch != NULL );
254
255         if ( bi->sql_dbuser == NULL ) {
256                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
257                         "user name not specified "
258                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
259                 return 1;
260         }
261         
262         if ( bi->sql_subtree_cond.bv_val == NULL ) {
263                 /*
264                  * Prepare concat function for subtree search condition
265                  */
266                 struct berval   concat;
267                 struct berval   values[] = {
268                         BER_BVC( "'%'" ),
269                         BER_BVC( "?" ),
270                         BER_BVNULL
271                 };
272                 struct berbuf   bb = BB_NULL;
273
274                 if ( backsql_prepare_pattern( bi->sql_concat_func, values, 
275                                 &concat ) ) {
276                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
277                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
278                         return 1;
279                 }
280                         
281                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
282                         "subtree search SQL condition not specified "
283                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
284                         0, 0, 0);
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, "blbbb",
293                                         &bi->sql_upper_func,
294                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
295                                                 "(ldap_entries.dn) LIKE ",
296                                         &bi->sql_upper_func_open,
297                                         &concat,
298                                         &bi->sql_upper_func_close );
299
300                 } else {
301
302                         /*
303                          * ldap_entries.dn LIKE CONCAT('%',?)
304                          */
305
306                         backsql_strfcat( &bb, "lb",
307                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
308                                                 "ldap_entries.dn LIKE ",
309                                         &concat );
310                 }
311
312                 bi->sql_subtree_cond = bb.bb_val;
313                         
314                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
315                         "setting \"%s\" as default\n",
316                         bi->sql_subtree_cond.bv_val, 0, 0 );
317         }
318
319         if ( bi->sql_children_cond.bv_val == NULL ) {
320                 struct berbuf   bb = BB_NULL;
321
322                 if ( bi->sql_upper_func.bv_val ) {
323
324                         /*
325                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
326                          */
327
328                         backsql_strfcat( &bb, "blbl",
329                                         &bi->sql_upper_func,
330                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
331                                                 "(ldap_entries.dn)=",
332                                         &bi->sql_upper_func,
333                                         (ber_len_t)STRLENOF( "(?)" ), "(?)" );
334
335                 } else {
336
337                         /*
338                          * ldap_entries.dn LIKE CONCAT('%,',?)
339                          */
340
341                         backsql_strfcat( &bb, "l",
342                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
343                                                 "ldap_entries.dn=?");
344                 }
345
346                 bi->sql_children_cond = bb.bb_val;
347                         
348                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
349                         "setting \"%s\" as default\n",
350                         bi->sql_children_cond.bv_val, 0, 0 );
351         }
352
353         if ( bi->sql_oc_query == NULL ) {
354                 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
355                         bi->sql_oc_query =
356                                 ch_strdup( backsql_def_needs_select_oc_query );
357
358                 } else {
359                         bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
360                 }
361
362                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
363                         "objectclass mapping SQL statement not specified "
364                         "(use \"oc_query\" directive in slapd.conf)\n", 
365                         0, 0, 0 );
366                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
367                         "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
368         }
369         
370         if ( bi->sql_at_query == NULL ) {
371                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
372                         "attribute mapping SQL statement not specified "
373                         "(use \"at_query\" directive in slapd.conf)\n",
374                         0, 0, 0 );
375                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
376                         "setting \"%s\" by default\n",
377                         backsql_def_at_query, 0, 0 );
378                 bi->sql_at_query = ch_strdup( backsql_def_at_query );
379         }
380         
381         if ( bi->sql_insentry_stmt == NULL ) {
382                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
383                         "entry insertion SQL statement not specified "
384                         "(use \"insentry_stmt\" directive in slapd.conf)\n",
385                         0, 0, 0 );
386                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
387                         "setting \"%s\" by default\n",
388                         backsql_def_insentry_stmt, 0, 0 );
389                 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
390         }
391         
392         if ( bi->sql_delentry_stmt == NULL ) {
393                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
394                         "entry deletion SQL statement not specified "
395                         "(use \"delentry_stmt\" directive in slapd.conf)\n",
396                         0, 0, 0 );
397                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
398                         "setting \"%s\" by default\n",
399                         backsql_def_delentry_stmt, 0, 0 );
400                 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
401         }
402
403         if ( bi->sql_renentry_stmt == NULL ) {
404                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
405                         "entry deletion SQL statement not specified "
406                         "(use \"renentry_stmt\" directive in slapd.conf)\n",
407                         0, 0, 0 );
408                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
409                         "setting \"%s\" by default\n",
410                         backsql_def_renentry_stmt, 0, 0 );
411                 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
412         }
413
414         if ( bi->sql_delobjclasses_stmt == NULL ) {
415                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
416                         "objclasses deletion SQL statement not specified "
417                         "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
418                         0, 0, 0 );
419                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
420                         "setting \"%s\" by default\n",
421                         backsql_def_delobjclasses_stmt, 0, 0 );
422                 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
423         }
424
425         op->o_hdr = (Opheader *)&op[ 1 ];
426         op->o_connid = (unsigned long)(-1);
427         op->o_bd = bd;
428         if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
429                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
430                         "connection failed, exiting\n", 0, 0, 0 );
431                 return 1;
432         }
433
434         /*
435          * Prepare ID selection query
436          */
437         if ( bi->sql_id_query == NULL ) {
438                 /* no custom id_query provided */
439                 if ( bi->sql_upper_func.bv_val == NULL ) {
440                         backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
441
442                 } else {
443                         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
444                                 backsql_strcat( &bb, backsql_id_query,
445                                                 "dn_ru=?", NULL );
446                         } else {
447                                 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
448                                         backsql_strfcat( &bb, "sbl",
449                                                         backsql_id_query,
450                                                         &bi->sql_upper_func, 
451                                                         (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
452                                 } else {
453                                         backsql_strfcat( &bb, "sblbcb",
454                                                         backsql_id_query,
455                                                         &bi->sql_upper_func, 
456                                                         (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
457                                                         &bi->sql_upper_func_open, 
458                                                         '?', 
459                                                         &bi->sql_upper_func_close );
460                                 }
461                         }
462                 }
463                 bi->sql_id_query = bb.bb_val.bv_val;
464         }
465
466         /*
467          * Prepare children ID selection query
468          */
469         BER_BVZERO( &bb.bb_val );
470         bb.bb_len = 0;
471         backsql_strfcat( &bb, "sbsb",
472                         "SELECT COUNT(distinct subordinates.id) "
473                         "FROM ldap_entries,ldap_entries ",
474                         &bi->sql_aliasing, "subordinates "
475                         "WHERE subordinates.parent=ldap_entries.id AND ",
476                         &bi->sql_children_cond );
477         bi->sql_has_children_query = bb.bb_val.bv_val;
478  
479         /*
480          * Prepare DN and objectClass aliasing bit of query
481          */
482         BER_BVZERO( &bb.bb_val );
483         bb.bb_len = 0;
484         backsql_strfcat( &bb, "sbbsbsbbsb",
485                         " ", &bi->sql_aliasing, &bi->sql_aliasing_quote,
486                         "objectClass", &bi->sql_aliasing_quote,
487                         ",ldap_entries.dn ", &bi->sql_aliasing,
488                         &bi->sql_aliasing_quote, "dn", &bi->sql_aliasing_quote );
489         bi->sql_dn_oc_aliasing = bb.bb_val;
490  
491         backsql_free_db_conn( op );
492         if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
493                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
494                         "test failed, schema map not loaded - exiting\n",
495                         0, 0, 0 );
496                 return 1;
497         }
498
499         /* should never happen! */
500         assert( bd->be_nsuffix != NULL );
501         
502         if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
503                 /* enable if only one suffix is defined */
504                 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
505         }
506
507         bi->sql_flags |= BSQLF_CHECK_SCHEMA;
508         
509         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
510                 "test succeeded, schema map loaded\n", 0, 0, 0 );
511         return 0;
512 }
513
514 int
515 backsql_db_close(
516         BackendDB       *bd )
517 {
518         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
519         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
520         return 0;
521 }
522
523 int
524 backsql_connection_destroy( Backend *bd, Connection *c )
525 {
526         char            opbuf[ OPERATION_BUFFER_SIZE ];
527         Operation*      op = (Operation *)opbuf;
528
529         op->o_hdr = (Opheader *)&op[ 1 ];
530         op->o_connid = c->c_connid;
531         op->o_bd = bd;
532
533         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
534         backsql_free_db_conn( op );
535         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
536
537         return 0;
538 }
539
540 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
541
542 /* conditionally define the init_module() function */
543 SLAP_BACKEND_INIT_MODULE( sql )
544
545 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
546