]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/util.c
d70b884da35b823628b49990c925e30c65465de1
[openldap] / servers / slapd / back-sql / util.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 #include "ac/ctype.h"
29 #include "ac/stdarg.h"
30
31 #include "slap.h"
32 #include "lber_pvt.h"
33 #include "ldap_pvt.h"
34 #include "proto-sql.h"
35
36 #define BACKSQL_MAX(a,b) ((a)>(b)?(a):(b))
37 #define BACKSQL_MIN(a,b) ((a)<(b)?(a):(b))
38
39 #define BACKSQL_STR_GROW 256
40
41 char backsql_def_oc_query[] = 
42         "SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return "
43         "FROM ldap_oc_mappings";
44 char backsql_def_needs_select_oc_query[] = 
45         "SELECT id,name,keytbl,keycol,create_proc,create_keyval,delete_proc,"
46         "expect_return FROM ldap_oc_mappings";
47 char backsql_def_at_query[] = 
48         "SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,"
49         "param_order,expect_return,sel_expr_u FROM ldap_attr_mappings "
50         "WHERE oc_map_id=?";
51 char backsql_def_delentry_query[] = "DELETE FROM ldap_entries WHERE id=?";
52 char backsql_def_insentry_query[] = 
53         "INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) "
54         "VALUES (?,?,?,?)";
55 char backsql_def_subtree_cond[] = "ldap_entries.dn LIKE CONCAT('%',?)";
56 char backsql_def_upper_subtree_cond[] = "(ldap_entries.dn) LIKE CONCAT('%',?)";
57 char backsql_id_query[] = "SELECT id,keyval,oc_map_id FROM ldap_entries WHERE ";
58 /* better ?||? or cast(?||? as varchar) */ 
59 char backsql_def_concat_func[] = "CONCAT(?,?)";
60
61 /* TimesTen */
62 char backsql_check_dn_ru_query[] = "SELECT dn_ru from ldap_entries";
63
64 struct berbuf *
65 backsql_strcat( struct berbuf *dest, ... )
66 {
67         va_list         strs;
68         ber_len_t       cdlen, cslen, grow;
69         char            *cstr;
70
71         assert( dest );
72         assert( dest->bb_val.bv_val == NULL 
73                         || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
74  
75 #ifdef BACKSQL_TRACE
76         Debug( LDAP_DEBUG_TRACE, "==>backsql_strcat()\n", 0, 0, 0 );
77 #endif /* BACKSQL_TRACE */
78
79         va_start( strs, dest );
80         if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
81                 dest->bb_val.bv_val = (char *)ch_calloc( BACKSQL_STR_GROW, 
82                                 sizeof( char ) );
83                 dest->bb_val.bv_len = 0;
84                 dest->bb_len = BACKSQL_STR_GROW;
85         }
86         cdlen = dest->bb_val.bv_len;
87         while ( ( cstr = va_arg( strs, char * ) ) != NULL ) {
88                 cslen = strlen( cstr );
89                 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
90                 if ( dest->bb_len - cdlen <= cslen ) {
91                         char    *tmp_dest;
92
93 #ifdef BACKSQL_TRACE
94                         Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
95                                 "buflen=%d, cdlen=%d, cslen=%d "
96                                 "-- reallocating dest\n",
97                                 dest->bb_len, cdlen + 1, cslen );
98 #endif /* BACKSQL_TRACE */
99
100                         tmp_dest = (char *)ch_realloc( dest->bb_val.bv_val,
101                                         ( dest->bb_len ) + grow * sizeof( char ) );
102                         if ( tmp_dest == NULL ) {
103                                 Debug( LDAP_DEBUG_ANY, "backsql_strcat(): "
104                                         "could not reallocate string buffer.\n",
105                                         0, 0, 0 );
106                                 return NULL;
107                         }
108                         dest->bb_val.bv_val = tmp_dest;
109                         dest->bb_len += grow;
110
111 #ifdef BACKSQL_TRACE
112                         Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
113                                 "new buflen=%d, dest=%p\n",
114                                 dest->bb_len, dest, 0 );
115 #endif /* BACKSQL_TRACE */
116                 }
117                 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
118                 cdlen += cslen;
119         }
120         va_end( strs );
121
122 #ifdef BACKSQL_TRACE
123         Debug( LDAP_DEBUG_TRACE, "<==backsql_strcat() (dest=\"%s\")\n", 
124                         dest->bb_val.bv_val, 0, 0 );
125 #endif /* BACKSQL_TRACE */
126
127         dest->bb_val.bv_len = cdlen;
128
129         return dest;
130
131
132 struct berbuf *
133 backsql_strfcat( struct berbuf *dest, const char *fmt, ... )
134 {
135         va_list         strs;
136         ber_len_t       cdlen;
137
138         assert( dest );
139         assert( fmt );
140         assert( dest->bb_len == 0 || dest->bb_len > dest->bb_val.bv_len );
141         assert( dest->bb_val.bv_val == NULL 
142                         || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
143  
144 #ifdef BACKSQL_TRACE
145         Debug( LDAP_DEBUG_TRACE, "==>backsql_strfcat()\n", 0, 0, 0 );
146 #endif /* BACKSQL_TRACE */
147
148         va_start( strs, fmt );
149         if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
150                 dest->bb_val.bv_val = (char *)ch_calloc( BACKSQL_STR_GROW, 
151                                 sizeof( char ) );
152                 dest->bb_val.bv_len = 0;
153                 dest->bb_len = BACKSQL_STR_GROW;
154         }
155
156         cdlen = dest->bb_val.bv_len;
157         for ( ; fmt[0]; fmt++ ) {
158                 ber_len_t       cslen, grow;
159                 char            *cstr, cc[ 2 ] = { '\0', '\0' };
160                 struct berval   *cbv;
161
162                 switch ( fmt[ 0 ] ) {
163
164                 /* berval */
165                 case 'b':
166                         cbv = va_arg( strs, struct berval * );
167                         cstr = cbv->bv_val;
168                         cslen = cbv->bv_len;
169                         break;
170
171                 /* length + string */
172                 case 'l':
173                         cslen = va_arg( strs, ber_len_t );
174                         cstr = va_arg( strs, char * );
175                         break;
176
177                 /* string */
178                 case 's':
179                         cstr = va_arg( strs, char * );
180                         cslen = strlen( cstr );
181                         break;
182
183                 /* char */
184                 case 'c':
185                         /* 
186                          * `char' is promoted to `int' when passed through `...'
187                          */
188                         cc[0] = va_arg( strs, int );
189                         cstr = cc;
190                         cslen = 1;
191                         break;
192
193                 default:
194                         assert( 0 );
195                 }
196
197                 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
198                 if ( dest->bb_len - cdlen <= cslen ) {
199                         char    *tmp_dest;
200
201 #ifdef BACKSQL_TRACE
202                         Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
203                                 "buflen=%d, cdlen=%d, cslen=%d "
204                                 "-- reallocating dest\n",
205                                 dest->bb_len, cdlen + 1, cslen );
206 #endif /* BACKSQL_TRACE */
207
208                         tmp_dest = (char *)ch_realloc( dest->bb_val.bv_val,
209                                         ( dest->bb_len ) + grow * sizeof( char ) );
210                         if ( tmp_dest == NULL ) {
211                                 Debug( LDAP_DEBUG_ANY, "backsql_strfcat(): "
212                                         "could not reallocate string buffer.\n",
213                                         0, 0, 0 );
214                                 return NULL;
215                         }
216                         dest->bb_val.bv_val = tmp_dest;
217                         dest->bb_len += grow * sizeof( char );
218
219 #ifdef BACKSQL_TRACE
220                         Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
221                                 "new buflen=%d, dest=%p\n", dest->bb_len, dest, 0 );
222 #endif /* BACKSQL_TRACE */
223                 }
224
225                 assert( cstr );
226                 
227                 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
228                 cdlen += cslen;
229         }
230
231         va_end( strs );
232
233 #ifdef BACKSQL_TRACE
234         Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest=\"%s\")\n", 
235                         dest->bb_val.bv_val, 0, 0 );
236 #endif /* BACKSQL_TRACE */
237
238         dest->bb_val.bv_len = cdlen;
239
240         return dest;
241
242
243 int
244 backsql_entry_addattr(
245         Entry           *e,
246         struct berval   *at_name,
247         struct berval   *at_val,
248         void            *memctx )
249 {
250         AttributeDescription    *ad;
251         int                     rc;
252         const char              *text;
253
254 #ifdef BACKSQL_TRACE
255         Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
256                 "at_name=\"%s\", at_val=\"%s\"\n", 
257                 at_name->bv_val, at_val->bv_val, 0 );
258 #endif /* BACKSQL_TRACE */
259
260         ad = NULL;
261         rc = slap_bv2ad( at_name, &ad, &text );
262         if ( rc != LDAP_SUCCESS ) {
263                 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
264                         "failed to find AttributeDescription for \"%s\"\n",
265                         at_name->bv_val, 0, 0 );
266                 return 0;
267         }
268
269         rc = attr_merge_normalize_one( e, ad, at_val, memctx );
270
271         if ( rc != 0 ) {
272                 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
273                         "failed to merge value \"%s\" for attribute \"%s\"\n",
274                         at_val->bv_val, at_name->bv_val, 0 );
275                 return 0;
276         }
277
278 #ifdef BACKSQL_TRACE
279         Debug( LDAP_DEBUG_TRACE, "<==backsql_query_addattr()\n", 0, 0, 0 );
280 #endif /* BACKSQL_TRACE */
281
282         return 1;
283 }
284
285 static char *
286 backsql_get_table_spec( char **p )
287 {
288         char            *s, *q;
289         struct berbuf   res = BB_NULL;
290
291         assert( p );
292         assert( *p );
293
294         s = *p;
295         while ( **p && **p != ',' ) {
296                 (*p)++;
297         }
298
299         if ( **p ) {
300                 *(*p)++ = '\0';
301         }
302         
303 #define BACKSQL_NEXT_WORD { \
304                 while ( *s && isspace( (unsigned char)*s ) ) s++; \
305                 if ( !*s ) return res.bb_val.bv_val; \
306                 q = s; \
307                 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
308                 if ( *q ) *q++='\0'; \
309         }
310
311         BACKSQL_NEXT_WORD;
312         /* table name */
313         backsql_strcat( &res, s, NULL );
314         s = q;
315
316         BACKSQL_NEXT_WORD;
317         if ( !strcasecmp( s, "as" ) ) {
318                 s = q;
319                 BACKSQL_NEXT_WORD;
320         }
321
322 #if 0
323         backsql_strcat( &res, " AS ", s, NULL );
324         /* oracle doesn't understand AS :( and other RDBMSes don't need it */
325 #endif
326
327         /* table alias */
328         backsql_strfcat( &res, "cs", ' ', s );
329
330         return res.bb_val.bv_val;
331 }
332
333 int
334 backsql_merge_from_clause( 
335         struct berbuf   *dest_from,
336         struct berval   *src_from )
337 {
338         char            *s, *p, *srcc, *pos, e;
339         struct berbuf   res = BB_NULL;
340
341 #ifdef BACKSQL_TRACE
342         Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
343                 "dest_from=\"%s\",src_from=\"%s\"\n",
344                 dest_from ? dest_from->bb_val.bv_val : "<NULL>",
345                 src_from->bv_val, 0 );
346 #endif /* BACKSQL_TRACE */
347
348         srcc = ch_strdup( src_from->bv_val );
349         p = srcc;
350
351         if ( dest_from != NULL ) {
352                 res = *dest_from;
353         }
354         
355         while ( *p ) {
356                 s = backsql_get_table_spec( &p );
357
358 #ifdef BACKSQL_TRACE
359                 Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
360                         "p=\"%s\" s=\"%s\"\n", p, s, 0 );
361 #endif /* BACKSQL_TRACE */
362
363                 if ( res.bb_val.bv_val == NULL ) {
364                         backsql_strcat( &res, s, NULL );
365
366                 } else {
367                         pos = strstr( res.bb_val.bv_val, s );
368                         if ( pos == NULL || ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) ) {
369                                 backsql_strfcat( &res, "cs", ',', s );
370                         }
371                 }
372                 
373                 if ( s ) {
374                         ch_free( s );
375                 }
376         }
377
378 #ifdef BACKSQL_TRACE
379         Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
380 #endif /* BACKSQL_TRACE */
381
382         free( srcc );
383         *dest_from = res;
384
385         return 1;
386 }
387
388 /*
389  * splits a pattern in components separated by '?'
390  * (double ?? are turned into single ? and left in the string)
391  * expected contains the number of expected occurrences of '?'
392  * (a negative value means parse as many as possible)
393  */
394
395 int
396 backsql_split_pattern(
397         const char      *_pattern,
398         BerVarray       *split_pattern,
399         int             expected )
400 {
401         char            *pattern, *start, *end;
402         struct berval   bv;
403         int             rc = 0;
404
405 #define SPLIT_CHAR      '?'
406         
407         assert( _pattern );
408         assert( split_pattern );
409
410         pattern = ch_strdup( _pattern );
411
412         start = pattern;
413         end = strchr( start, SPLIT_CHAR );
414         for ( ; start; expected-- ) {
415                 char            *real_end = end;
416                 ber_len_t       real_len;
417                 
418                 if ( real_end == NULL ) {
419                         real_end = start + strlen( start );
420
421                 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
422                         expected++;
423                         AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
424                         end = strchr( real_end + 1, SPLIT_CHAR );
425                         continue;
426                 }
427
428                 real_len = real_end - start;
429                 if ( real_len == 0 ) {
430                         ber_str2bv( "", 0, 1, &bv );
431                 } else {
432                         ber_str2bv( start, real_len, 1, &bv );
433                 }
434
435                 ber_bvarray_add( split_pattern, &bv );
436
437                 if ( expected == 0 ) {
438                         if ( end != NULL ) {
439                                 rc = -1;
440                                 goto done;
441                         }
442                         break;
443                 }
444
445                 if ( end != NULL ) {
446                         start = end + 1;
447                         end = strchr( start, SPLIT_CHAR );
448                 }
449         }
450
451 done:;
452
453         ch_free( pattern );
454
455         return rc;
456 }
457
458 int
459 backsql_prepare_pattern(
460         BerVarray       split_pattern,
461         BerVarray       values,
462         struct berval   *res )
463 {
464         int             i;
465         struct berbuf   bb = BB_NULL;
466
467         assert( res );
468
469         for ( i = 0; values[i].bv_val; i++ ) {
470                 if ( split_pattern[i].bv_val == NULL ) {
471                         ch_free( bb.bb_val.bv_val );
472                         return -1;
473                 }
474                 backsql_strfcat( &bb, "b", &split_pattern[ i ] );
475                 backsql_strfcat( &bb, "b", &values[ i ] );
476         }
477
478         if ( split_pattern[ i ].bv_val == NULL ) {
479                 ch_free( bb.bb_val.bv_val );
480                 return -1;
481         }
482
483         backsql_strfcat( &bb, "b", &split_pattern[ i ] );
484
485         *res = bb.bb_val;
486
487         return 0;
488 }
489
490 #endif /* SLAPD_SQL */
491