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