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