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