]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/util.c
ffd10efc9516e7f813661025ee440e895a0aeb3f
[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                 AC_MEMCPY( dest->bv_val + cdlen, cstr, cslen + 1 );
216                 cdlen += cslen;
217         }
218
219         va_end( strs );
220
221 #ifdef BACKSQL_TRACE
222         Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest='%s')\n", 
223                         dest, 0, 0 );
224 #endif /* BACKSQL_TRACE */
225
226         dest->bv_len = cdlen;
227
228         return dest;
229
230
231 int
232 backsql_entry_addattr(
233         Entry           *e,
234         struct berval   *at_name,
235         struct berval   *at_val )
236 {
237         AttributeDescription    *ad;
238         int                     rc;
239         const char              *text;
240
241 #ifdef BACKSQL_TRACE
242         Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
243                 "at_name='%s', at_val='%s'\n", 
244                 at_name->bv_val, at_val->bv_val, 0 );
245 #endif /* BACKSQL_TRACE */
246
247         ad = NULL;
248         rc = slap_bv2ad( at_name, &ad, &text );
249         if ( rc != LDAP_SUCCESS ) {
250                 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
251                         "failed to find AttributeDescription for '%s'\n",
252                         at_name->bv_val, 0, 0 );
253                 return 0;
254         }
255
256         rc = attr_merge_one( e, ad, at_val );
257
258         if ( rc != 0 ) {
259                 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(): "
260                         "failed to merge value '%s' for attribute '%s'\n",
261                         at_val->bv_val, at_name->bv_val, 0 );
262                 return 0;
263         }
264
265 #ifdef BACKSQL_TRACE
266         Debug( LDAP_DEBUG_TRACE, "<==backsql_query_addattr()\n", 0, 0, 0 );
267 #endif /* BACKSQL_TRACE */
268
269         return 1;
270 }
271
272 char *
273 backsql_get_table_spec( char **p )
274 {
275         char            *s, *q;
276         struct berval   res = { 0, NULL };
277         ber_len_t       res_len = 0;
278
279         assert( p );
280         assert( *p );
281
282         s = *p;
283         while ( **p && **p != ',' ) {
284                 (*p)++;
285         }
286
287         if ( **p ) {
288                 *(*p)++ = '\0';
289         }
290         
291 #define BACKSQL_NEXT_WORD { \
292                 while ( *s && isspace( (unsigned char)*s ) ) s++; \
293                 if ( !*s ) return res.bv_val; \
294                 q = s; \
295                 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
296                 if ( *q ) *q++='\0'; \
297         }
298
299         BACKSQL_NEXT_WORD;
300         /* table name */
301         backsql_strcat( &res, &res_len, s, NULL );
302         s = q;
303
304         BACKSQL_NEXT_WORD;
305         if ( !strcasecmp( s, "as" ) ) {
306                 s = q;
307                 BACKSQL_NEXT_WORD;
308         }
309
310 #if 0
311         backsql_strcat( &res, &res_len, " AS ", s, NULL );
312         /* oracle doesn't understand AS :( */
313 #endif
314
315         /* table alias */
316         backsql_strfcat( &res, &res_len, "cs", ' ', s );
317
318         return res.bv_val;
319 }
320
321 int
322 backsql_merge_from_clause( 
323         struct berval   *dest_from,
324         ber_len_t       *dest_len, 
325         struct berval   *src_from )
326 {
327         char            *s, *p, *srcc, *pos, e;
328         struct berval   res = { 0 , NULL };
329
330 #ifdef BACKSQL_TRACE
331         Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
332                 "dest_from='%s',src_from='%s'\n",
333                 dest_from ? dest_from->bv_val : "<NULL>", src_from, 0 );
334 #endif /* BACKSQL_TRACE */
335
336         srcc = ch_strdup( src_from->bv_val );
337         p = srcc;
338
339         if ( dest_from != NULL ) {
340                 res = *dest_from;
341         }
342         
343         while ( *p ) {
344                 s = backsql_get_table_spec( &p );
345
346 #ifdef BACKSQL_TRACE
347                 Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
348                         "p='%s' s='%s'\n", p, s, 0 );
349 #endif /* BACKSQL_TRACE */
350
351                 if ( res.bv_val == NULL ) {
352                         backsql_strcat( &res, dest_len, s, NULL );
353
354                 } else {
355                         pos = strstr( res.bv_val, s );
356                         if ( pos == NULL ) {
357                                 backsql_strfcat( &res, dest_len, "cs", ',', s );
358                         } else if ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) {
359                                 backsql_strfcat( &res, dest_len, "cs", ',', s );
360                         }
361                 }
362                 
363                 if ( s ) {
364                         ch_free( s );
365                 }
366         }
367
368 #ifdef BACKSQL_TRACE
369         Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
370 #endif /* BACKSQL_TRACE */
371
372         free( srcc );
373         *dest_from = res;
374
375         return 1;
376 }
377
378 /*
379  * splits a pattern in components separated by '?'
380  * (double ?? are turned into single ? and left in the string)
381  * expected contains the number of expected occurrences of '?'
382  * (a negative value means parse as many as possible)
383  */
384
385 int
386 backsql_split_pattern(
387         const char      *_pattern,
388         BerVarray       *split_pattern,
389         int             expected )
390 {
391         char            *pattern, *start, *end;
392         struct berval   bv;
393         int             rc = 0;
394
395 #define SPLIT_CHAR      '?'
396         
397         assert( _pattern );
398         assert( split_pattern );
399
400         pattern = ch_strdup( _pattern );
401
402         start = pattern;
403         end = strchr( start, SPLIT_CHAR );
404         for ( ; start; expected-- ) {
405                 char            *real_end = end;
406                 ber_len_t       real_len;
407                 
408                 if ( real_end == NULL ) {
409                         real_end = start + strlen( start );
410
411                 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
412                         expected++;
413                         AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
414                         end = strchr( real_end + 1, SPLIT_CHAR );
415                         continue;
416                 }
417
418                 real_len = real_end - start;
419                 if ( real_len == 0 ) {
420                         ber_str2bv( "", 0, 1, &bv );
421                 } else {
422                         ber_str2bv( start, real_len, 1, &bv );
423                 }
424
425                 ber_bvarray_add( split_pattern, &bv );
426
427                 if ( expected == 0 ) {
428                         if ( end != NULL ) {
429                                 rc = -1;
430                                 goto done;
431                         }
432                         break;
433                 }
434
435                 if ( end != NULL ) {
436                         start = end + 1;
437                         end = strchr( start, SPLIT_CHAR );
438                 }
439         }
440
441 done:;
442
443         ch_free( pattern );
444
445         return rc;
446 }
447
448 int
449 backsql_prepare_pattern(
450         BerVarray       split_pattern,
451         BerVarray       values,
452         struct berval   *res )
453 {
454         ber_len_t       len = 0;
455         int             i;
456
457         res->bv_val = NULL;
458         res->bv_len = 0;
459
460         for ( i = 0; values[i].bv_val; i++ ) {
461                 if ( split_pattern[i].bv_val == NULL ) {
462                         return -1;
463                 }
464                 backsql_strfcat( res, &len, "b", &split_pattern[ i ] );
465                 backsql_strfcat( res, &len, "b", &values[ i ] );
466         }
467
468         if ( split_pattern[ i ].bv_val == NULL ) {
469                 return -1;
470         }
471
472         backsql_strfcat( res, &len, "b", &split_pattern[ i ] );
473
474         return 0;
475 }
476
477 #endif /* SLAPD_SQL */
478