]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/init.c
fix ITS#3753
[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 LDAP_CONTROL_X_TREE_DELETE
43                 LDAP_CONTROL_X_TREE_DELETE,
44 #endif /* LDAP_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  
78         bi->bi_connection_init = 0;
79         bi->bi_connection_destroy = backsql_connection_destroy;
80
81         Debug( LDAP_DEBUG_TRACE,"<==sql_back_initialize()\n", 0, 0, 0 );
82         return 0;
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_malloc( 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_stmt );
145         free( bi->sql_delentry_stmt );
146         free( bi->sql_renentry_stmt );
147         free( bi->sql_delobjclasses_stmt );
148
149         if ( bi->sql_anlist ) {
150                 int     i;
151
152                 for ( i = 0; !BER_BVISNULL( &bi->sql_anlist[i].an_name ); i++ )
153                 {
154                         ch_free( bi->sql_anlist[i].an_name.bv_val );
155                 }
156                 ch_free( bi->sql_anlist );
157         }
158
159         if ( bi->sql_baseObject ) {
160                 entry_free( bi->sql_baseObject );
161         }
162         
163         free( bi );
164         
165         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_destroy()\n", 0, 0, 0 );
166         return 0;
167 }
168
169 int
170 backsql_db_open(
171         BackendDB       *bd )
172 {
173         backsql_info    *bi = (backsql_info*)bd->be_private;
174         SQLHDBC         dbh = SQL_NULL_HDBC;
175         struct berbuf   bb = BB_NULL;
176
177         char            opbuf[ OPERATION_BUFFER_SIZE ];
178         Operation*      op = (Operation *)opbuf;
179         
180         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_open(): "
181                 "testing RDBMS connection\n", 0, 0, 0 );
182         if ( bi->sql_dbname == NULL ) {
183                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
184                         "datasource name not specified "
185                         "(use \"dbname\" directive in slapd.conf)\n", 0, 0, 0 );
186                 return 1;
187         }
188
189         if ( bi->sql_concat_func == NULL ) {
190                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
191                         "concat func not specified (use \"concat_pattern\" "
192                         "directive in slapd.conf)\n", 0, 0, 0 );
193
194                 if ( backsql_split_pattern( backsql_def_concat_func, 
195                                 &bi->sql_concat_func, 2 ) ) {
196                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
197                                 "unable to parse pattern \"%s\"",
198                                 backsql_def_concat_func, 0, 0 );
199                         return 1;
200                 }
201         }
202
203         /*
204          * Prepare cast string as required
205          */
206         if ( bi->sql_upper_func.bv_val ) {
207                 char buf[1024];
208
209                 if ( BACKSQL_UPPER_NEEDS_CAST( bi ) ) {
210                         snprintf( buf, sizeof( buf ), 
211                                 "%s(cast (" /* ? as varchar(%d))) */ , 
212                                 bi->sql_upper_func.bv_val );
213                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
214
215                         snprintf( buf, sizeof( buf ),
216                                 /* (cast(? */ " as varchar(%d)))",
217                                 BACKSQL_MAX_DN_LEN );
218                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_close );
219
220                 } else {
221                         snprintf( buf, sizeof( buf ), "%s(" /* ?) */ ,
222                                         bi->sql_upper_func.bv_val );
223                         ber_str2bv( buf, 0, 1, &bi->sql_upper_func_open );
224
225                         ber_str2bv( /* (? */ ")", 0, 1, &bi->sql_upper_func_close );
226                 }
227         }
228
229         /* normalize filter values only if necessary */
230         bi->sql_caseIgnoreMatch = mr_find( "caseIgnoreMatch" );
231         assert( bi->sql_caseIgnoreMatch );
232
233         bi->sql_telephoneNumberMatch = mr_find( "telephoneNumberMatch" );
234         assert( bi->sql_telephoneNumberMatch );
235
236         if ( bi->sql_dbuser == NULL ) {
237                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
238                         "user name not specified "
239                         "(use \"dbuser\" directive in slapd.conf)\n", 0, 0, 0 );
240                 return 1;
241         }
242         
243         if ( bi->sql_subtree_cond.bv_val == NULL ) {
244                 /*
245                  * Prepare concat function for subtree search condition
246                  */
247                 struct berval   concat;
248                 struct berval   values[] = {
249                         BER_BVC( "'%'" ),
250                         BER_BVC( "?" ),
251                         BER_BVNULL
252                 };
253                 struct berbuf   bb = BB_NULL;
254
255                 if ( backsql_prepare_pattern( bi->sql_concat_func, values, 
256                                 &concat ) ) {
257                         Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
258                                 "unable to prepare CONCAT pattern", 0, 0, 0 );
259                         return 1;
260                 }
261                         
262                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
263                         "subtree search SQL condition not specified "
264                         "(use \"subtree_cond\" directive in slapd.conf)\n", 
265                         0, 0, 0);
266
267                 if ( bi->sql_upper_func.bv_val ) {
268
269                         /*
270                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%',?))
271                          */
272
273                         backsql_strfcat( &bb, "blbbb",
274                                         &bi->sql_upper_func,
275                                         (ber_len_t)STRLENOF( "(ldap_entries.dn) LIKE " ),
276                                                 "(ldap_entries.dn) LIKE ",
277                                         &bi->sql_upper_func_open,
278                                         &concat,
279                                         &bi->sql_upper_func_close );
280
281                 } else {
282
283                         /*
284                          * ldap_entries.dn LIKE CONCAT('%',?)
285                          */
286
287                         backsql_strfcat( &bb, "lb",
288                                         (ber_len_t)STRLENOF( "ldap_entries.dn LIKE " ),
289                                                 "ldap_entries.dn LIKE ",
290                                         &concat );
291                 }
292
293                 bi->sql_subtree_cond = bb.bb_val;
294                         
295                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
296                         "setting \"%s\" as default\n",
297                         bi->sql_subtree_cond.bv_val, 0, 0 );
298         }
299
300         if ( bi->sql_children_cond.bv_val == NULL ) {
301                 struct berbuf   bb = BB_NULL;
302
303                 if ( bi->sql_upper_func.bv_val ) {
304
305                         /*
306                          * UPPER(ldap_entries.dn) LIKE UPPER(CONCAT('%,',?))
307                          */
308
309                         backsql_strfcat( &bb, "blbl",
310                                         &bi->sql_upper_func,
311                                         (ber_len_t)STRLENOF( "(ldap_entries.dn)=" ),
312                                                 "(ldap_entries.dn)=",
313                                         &bi->sql_upper_func,
314                                         (ber_len_t)STRLENOF( "(?)" ), "(?)" );
315
316                 } else {
317
318                         /*
319                          * ldap_entries.dn LIKE CONCAT('%,',?)
320                          */
321
322                         backsql_strfcat( &bb, "l",
323                                         (ber_len_t)STRLENOF( "ldap_entries.dn=?" ),
324                                                 "ldap_entries.dn=?");
325                 }
326
327                 bi->sql_children_cond = bb.bb_val;
328                         
329                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
330                         "setting \"%s\" as default\n",
331                         bi->sql_children_cond.bv_val, 0, 0 );
332         }
333
334         if ( bi->sql_oc_query == NULL ) {
335                 if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
336                         bi->sql_oc_query =
337                                 ch_strdup( backsql_def_needs_select_oc_query );
338
339                 } else {
340                         bi->sql_oc_query = ch_strdup( backsql_def_oc_query );
341                 }
342
343                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
344                         "objectclass mapping SQL statement not specified "
345                         "(use \"oc_query\" directive in slapd.conf)\n", 
346                         0, 0, 0 );
347                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
348                         "setting \"%s\" by default\n", bi->sql_oc_query, 0, 0 );
349         }
350         
351         if ( bi->sql_at_query == NULL ) {
352                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
353                         "attribute mapping SQL statement not specified "
354                         "(use \"at_query\" directive in slapd.conf)\n",
355                         0, 0, 0 );
356                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
357                         "setting \"%s\" by default\n",
358                         backsql_def_at_query, 0, 0 );
359                 bi->sql_at_query = ch_strdup( backsql_def_at_query );
360         }
361         
362         if ( bi->sql_insentry_stmt == NULL ) {
363                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
364                         "entry insertion SQL statement not specified "
365                         "(use \"insentry_stmt\" directive in slapd.conf)\n",
366                         0, 0, 0 );
367                 Debug(LDAP_DEBUG_TRACE, "backsql_db_open(): "
368                         "setting \"%s\" by default\n",
369                         backsql_def_insentry_stmt, 0, 0 );
370                 bi->sql_insentry_stmt = ch_strdup( backsql_def_insentry_stmt );
371         }
372         
373         if ( bi->sql_delentry_stmt == NULL ) {
374                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
375                         "entry deletion SQL statement not specified "
376                         "(use \"delentry_stmt\" directive in slapd.conf)\n",
377                         0, 0, 0 );
378                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
379                         "setting \"%s\" by default\n",
380                         backsql_def_delentry_stmt, 0, 0 );
381                 bi->sql_delentry_stmt = ch_strdup( backsql_def_delentry_stmt );
382         }
383
384         if ( bi->sql_renentry_stmt == NULL ) {
385                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
386                         "entry deletion SQL statement not specified "
387                         "(use \"renentry_stmt\" directive in slapd.conf)\n",
388                         0, 0, 0 );
389                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
390                         "setting \"%s\" by default\n",
391                         backsql_def_renentry_stmt, 0, 0 );
392                 bi->sql_renentry_stmt = ch_strdup( backsql_def_renentry_stmt );
393         }
394
395         if ( bi->sql_delobjclasses_stmt == NULL ) {
396                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
397                         "objclasses deletion SQL statement not specified "
398                         "(use \"delobjclasses_stmt\" directive in slapd.conf)\n",
399                         0, 0, 0 );
400                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
401                         "setting \"%s\" by default\n",
402                         backsql_def_delobjclasses_stmt, 0, 0 );
403                 bi->sql_delobjclasses_stmt = ch_strdup( backsql_def_delobjclasses_stmt );
404         }
405
406         op->o_hdr = (Opheader *)&op[ 1 ];
407         op->o_connid = (unsigned long)(-1);
408         op->o_bd = bd;
409         if ( backsql_get_db_conn( op, &dbh ) != LDAP_SUCCESS ) {
410                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
411                         "connection failed, exiting\n", 0, 0, 0 );
412                 return 1;
413         }
414
415         /*
416          * Prepare ID selection query
417          */
418         if ( bi->sql_id_query == NULL ) {
419                 /* no custom id_query provided */
420                 if ( bi->sql_upper_func.bv_val == NULL ) {
421                         backsql_strcat( &bb, backsql_id_query, "dn=?", NULL );
422
423                 } else {
424                         if ( BACKSQL_HAS_LDAPINFO_DN_RU( bi ) ) {
425                                 backsql_strcat( &bb, backsql_id_query,
426                                                 "dn_ru=?", NULL );
427                         } else {
428                                 if ( BACKSQL_USE_REVERSE_DN( bi ) ) {
429                                         backsql_strfcat( &bb, "sbl",
430                                                         backsql_id_query,
431                                                         &bi->sql_upper_func, 
432                                                         (ber_len_t)STRLENOF( "(dn)=?" ), "(dn)=?" );
433                                 } else {
434                                         backsql_strfcat( &bb, "sblbcb",
435                                                         backsql_id_query,
436                                                         &bi->sql_upper_func, 
437                                                         (ber_len_t)STRLENOF( "(dn)=" ), "(dn)=",
438                                                         &bi->sql_upper_func_open, 
439                                                         '?', 
440                                                         &bi->sql_upper_func_close );
441                                 }
442                         }
443                 }
444                 bi->sql_id_query = bb.bb_val.bv_val;
445         }
446
447         /*
448          * Prepare children ID selection query
449          */
450         bi->sql_has_children_query = NULL;
451
452         bb.bb_val.bv_val = NULL;
453         bb.bb_val.bv_len = 0;
454         bb.bb_len = 0;
455         backsql_strfcat( &bb, "sb",
456                         "SELECT COUNT(distinct subordinates.id) "
457                         "FROM ldap_entries,ldap_entries " BACKSQL_ALIASING "subordinates "
458                         "WHERE subordinates.parent=ldap_entries.id AND ",
459                         &bi->sql_children_cond );
460         bi->sql_has_children_query = bb.bb_val.bv_val;
461  
462         backsql_free_db_conn( op );
463         if ( !BACKSQL_SCHEMA_LOADED( bi ) ) {
464                 Debug( LDAP_DEBUG_TRACE, "backsql_db_open(): "
465                         "test failed, schema map not loaded - exiting\n",
466                         0, 0, 0 );
467                 return 1;
468         }
469
470         /* should never happen! */
471         assert( bd->be_nsuffix != NULL );
472         
473         if ( BER_BVISNULL( &bd->be_nsuffix[ 1 ] ) ) {
474                 /* enable if only one suffix is defined */
475                 bi->sql_flags |= BSQLF_USE_SUBTREE_SHORTCUT;
476         }
477         bi->sql_flags |= BSQLF_CHECK_SCHEMA;
478         
479         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_open(): "
480                 "test succeeded, schema map loaded\n", 0, 0, 0 );
481         return 0;
482 }
483
484 int
485 backsql_db_close(
486         BackendDB       *bd )
487 {
488         Debug( LDAP_DEBUG_TRACE, "==>backsql_db_close()\n", 0, 0, 0 );
489         Debug( LDAP_DEBUG_TRACE, "<==backsql_db_close()\n", 0, 0, 0 );
490         return 0;
491 }
492
493 int
494 backsql_connection_destroy( Backend *bd, Connection *c )
495 {
496         char            opbuf[ OPERATION_BUFFER_SIZE ];
497         Operation*      op = (Operation *)opbuf;
498
499         op->o_hdr = (Opheader *)&op[ 1 ];
500         op->o_connid = c->c_connid;
501         op->o_bd = bd;
502
503         Debug( LDAP_DEBUG_TRACE, "==>backsql_connection_destroy()\n", 0, 0, 0 );
504         backsql_free_db_conn( op );
505         Debug( LDAP_DEBUG_TRACE, "<==backsql_connection_destroy()\n", 0, 0, 0 );
506
507         return 0;
508 }
509
510 #if SLAPD_SQL == SLAPD_MOD_DYNAMIC
511
512 /* conditionally define the init_module() function */
513 SLAP_BACKEND_INIT_MODULE( sql )
514
515 #endif /* SLAPD_SQL == SLAPD_MOD_DYNAMIC */
516