2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2015 The OpenLDAP Foundation.
5 * Portions Copyright 1999 Dmitry Kovalev.
6 * Portions Copyright 2002 Pierangelo Masarati.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
20 * Pierangelo Masarati.
26 #include <sys/types.h>
27 #include "ac/string.h"
29 #include "ac/stdarg.h"
32 #include "proto-sql.h"
35 #define BACKSQL_MAX(a,b) ((a)>(b)?(a):(b))
36 #define BACKSQL_MIN(a,b) ((a)<(b)?(a):(b))
38 #define BACKSQL_STR_GROW 256
40 const char backsql_def_oc_query[] =
41 "SELECT id,name,keytbl,keycol,create_proc,delete_proc,expect_return "
42 "FROM ldap_oc_mappings";
43 const char backsql_def_needs_select_oc_query[] =
44 "SELECT id,name,keytbl,keycol,create_proc,create_keyval,delete_proc,"
45 "expect_return FROM ldap_oc_mappings";
46 const char backsql_def_at_query[] =
47 "SELECT name,sel_expr,from_tbls,join_where,add_proc,delete_proc,"
48 "param_order,expect_return,sel_expr_u FROM ldap_attr_mappings "
50 const char backsql_def_delentry_stmt[] = "DELETE FROM ldap_entries WHERE id=?";
51 const char backsql_def_renentry_stmt[] =
52 "UPDATE ldap_entries SET dn=?,parent=?,keyval=? WHERE id=?";
53 const char backsql_def_insentry_stmt[] =
54 "INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) "
56 const char backsql_def_delobjclasses_stmt[] = "DELETE FROM ldap_entry_objclasses "
58 const char backsql_def_subtree_cond[] = "ldap_entries.dn LIKE CONCAT('%',?)";
59 const char backsql_def_upper_subtree_cond[] = "(ldap_entries.dn) LIKE CONCAT('%',?)";
60 const char backsql_id_query[] = "SELECT id,keyval,oc_map_id,dn FROM ldap_entries WHERE ";
61 /* better ?||? or cast(?||? as varchar) */
62 const char backsql_def_concat_func[] = "CONCAT(?,?)";
65 const char backsql_check_dn_ru_query[] = "SELECT dn_ru FROM ldap_entries";
68 backsql_strcat_x( struct berbuf *dest, void *memctx, ... )
71 ber_len_t cdlen, cslen, grow;
74 assert( dest != NULL );
75 assert( dest->bb_val.bv_val == NULL
76 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
79 Debug( LDAP_DEBUG_TRACE, "==>backsql_strcat()\n", 0, 0, 0 );
80 #endif /* BACKSQL_TRACE */
82 va_start( strs, memctx );
83 if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
84 dest->bb_val.bv_val = (char *)ber_memalloc_x( BACKSQL_STR_GROW * sizeof( char ), memctx );
85 dest->bb_val.bv_len = 0;
86 dest->bb_len = BACKSQL_STR_GROW;
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 ) {
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 */
102 tmp_dest = (char *)ber_memrealloc_x( dest->bb_val.bv_val,
103 dest->bb_len + grow * sizeof( char ), memctx );
104 if ( tmp_dest == NULL ) {
105 Debug( LDAP_DEBUG_ANY, "backsql_strcat(): "
106 "could not reallocate string buffer.\n",
111 dest->bb_val.bv_val = tmp_dest;
112 dest->bb_len += grow;
115 Debug( LDAP_DEBUG_TRACE, "backsql_strcat(): "
116 "new buflen=%d, dest=%p\n",
117 dest->bb_len, dest, 0 );
118 #endif /* BACKSQL_TRACE */
120 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
126 Debug( LDAP_DEBUG_TRACE, "<==backsql_strcat() (dest=\"%s\")\n",
127 dest->bb_val.bv_val, 0, 0 );
128 #endif /* BACKSQL_TRACE */
130 dest->bb_val.bv_len = cdlen;
136 backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... )
141 assert( dest != NULL );
142 assert( fmt != NULL );
143 assert( dest->bb_len == 0 || dest->bb_len > dest->bb_val.bv_len );
144 assert( dest->bb_val.bv_val == NULL
145 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
148 Debug( LDAP_DEBUG_TRACE, "==>backsql_strfcat()\n", 0, 0, 0 );
149 #endif /* BACKSQL_TRACE */
151 va_start( strs, fmt );
152 if ( dest->bb_val.bv_val == NULL || dest->bb_len == 0 ) {
153 dest->bb_val.bv_val = (char *)ber_memalloc_x( BACKSQL_STR_GROW * sizeof( char ), memctx );
154 dest->bb_val.bv_len = 0;
155 dest->bb_len = BACKSQL_STR_GROW;
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' };
164 switch ( fmt[ 0 ] ) {
168 cbv = va_arg( strs, struct berval * );
173 /* length + string */
175 cslen = va_arg( strs, ber_len_t );
176 cstr = va_arg( strs, char * );
181 cstr = va_arg( strs, char * );
182 cslen = strlen( cstr );
188 * `char' is promoted to `int' when passed through `...'
190 cc[0] = va_arg( strs, int );
199 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
200 if ( dest->bb_len - cdlen <= cslen ) {
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 */
210 tmp_dest = (char *)ber_memrealloc_x( dest->bb_val.bv_val,
211 ( dest->bb_len ) + grow * sizeof( char ), memctx );
212 if ( tmp_dest == NULL ) {
213 Debug( LDAP_DEBUG_ANY, "backsql_strfcat(): "
214 "could not reallocate string buffer.\n",
219 dest->bb_val.bv_val = tmp_dest;
220 dest->bb_len += grow * sizeof( char );
223 Debug( LDAP_DEBUG_TRACE, "backsql_strfcat(): "
224 "new buflen=%d, dest=%p\n", dest->bb_len, dest, 0 );
225 #endif /* BACKSQL_TRACE */
228 assert( cstr != NULL );
230 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
237 Debug( LDAP_DEBUG_TRACE, "<==backsql_strfcat() (dest=\"%s\")\n",
238 dest->bb_val.bv_val, 0, 0 );
239 #endif /* BACKSQL_TRACE */
241 dest->bb_val.bv_len = cdlen;
247 backsql_entry_addattr(
249 AttributeDescription *ad,
256 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(\"%s\"): %s=%s\n",
257 e->e_name.bv_val, ad->ad_cname.bv_val, val->bv_val );
258 #endif /* BACKSQL_TRACE */
260 rc = attr_merge_normalize_one( e, ad, val, memctx );
262 if ( rc != LDAP_SUCCESS ) {
263 Debug( LDAP_DEBUG_TRACE, "backsql_entry_addattr(\"%s\"): "
264 "failed to merge value \"%s\" for attribute \"%s\"\n",
265 e->e_name.bv_val, val->bv_val, ad->ad_cname.bv_val );
270 Debug( LDAP_DEBUG_TRACE, "<==backsql_entry_addattr(\"%s\")\n",
271 e->e_name.bv_val, 0, 0 );
272 #endif /* BACKSQL_TRACE */
278 backsql_get_table_spec( backsql_info *bi, char **p )
281 struct berbuf res = BB_NULL;
284 assert( *p != NULL );
287 while ( **p && **p != ',' ) {
295 #define BACKSQL_NEXT_WORD { \
296 while ( *s && isspace( (unsigned char)*s ) ) s++; \
297 if ( !*s ) return res.bb_val.bv_val; \
299 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
300 if ( *q ) *q++='\0'; \
305 backsql_strcat_x( &res, NULL, s, NULL );
309 if ( strcasecmp( s, "AS" ) == 0 ) {
314 /* oracle doesn't understand "AS" :( and other RDBMSes don't need it */
315 backsql_strfcat_x( &res, NULL, "lbbsb",
316 STRLENOF( " " ), " ",
318 &bi->sql_aliasing_quote,
320 &bi->sql_aliasing_quote );
322 return res.bb_val.bv_val;
326 backsql_merge_from_clause(
328 struct berbuf *dest_from,
329 struct berval *src_from )
331 char *s, *p, *srcc, *pos, e;
332 struct berbuf res = BB_NULL;
335 Debug( LDAP_DEBUG_TRACE, "==>backsql_merge_from_clause(): "
336 "dest_from=\"%s\",src_from=\"%s\"\n",
337 dest_from ? dest_from->bb_val.bv_val : "<NULL>",
338 src_from->bv_val, 0 );
339 #endif /* BACKSQL_TRACE */
341 srcc = ch_strdup( src_from->bv_val );
344 if ( dest_from != NULL ) {
349 s = backsql_get_table_spec( bi, &p );
352 Debug( LDAP_DEBUG_TRACE, "backsql_merge_from_clause(): "
353 "p=\"%s\" s=\"%s\"\n", p, s, 0 );
354 #endif /* BACKSQL_TRACE */
356 if ( BER_BVISNULL( &res.bb_val ) ) {
357 backsql_strcat_x( &res, NULL, s, NULL );
360 pos = strstr( res.bb_val.bv_val, s );
361 if ( pos == NULL || ( ( e = pos[ strlen( s ) ] ) != '\0' && e != ',' ) ) {
362 backsql_strfcat_x( &res, NULL, "cs", ',', s );
372 Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
373 #endif /* BACKSQL_TRACE */
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)
389 backsql_split_pattern(
390 const char *_pattern,
391 BerVarray *split_pattern,
394 char *pattern, *start, *end;
398 #define SPLIT_CHAR '?'
400 assert( _pattern != NULL );
401 assert( split_pattern != NULL );
403 pattern = ch_strdup( _pattern );
406 end = strchr( start, SPLIT_CHAR );
407 for ( ; start; expected-- ) {
408 char *real_end = end;
411 if ( real_end == NULL ) {
412 real_end = start + strlen( start );
414 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
416 AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
417 end = strchr( real_end + 1, SPLIT_CHAR );
421 real_len = real_end - start;
422 if ( real_len == 0 ) {
423 ber_str2bv( "", 0, 1, &bv );
425 ber_str2bv( start, real_len, 1, &bv );
428 ber_bvarray_add( split_pattern, &bv );
430 if ( expected == 0 ) {
440 end = strchr( start, SPLIT_CHAR );
452 backsql_prepare_pattern(
453 BerVarray split_pattern,
458 struct berbuf bb = BB_NULL;
460 assert( res != NULL );
462 for ( i = 0; values[i].bv_val; i++ ) {
463 if ( split_pattern[i].bv_val == NULL ) {
464 ch_free( bb.bb_val.bv_val );
467 backsql_strfcat_x( &bb, NULL, "b", &split_pattern[ i ] );
468 backsql_strfcat_x( &bb, NULL, "b", &values[ i ] );
471 if ( split_pattern[ i ].bv_val == NULL ) {
472 ch_free( bb.bb_val.bv_val );
476 backsql_strfcat_x( &bb, NULL, "b", &split_pattern[ i ] );
487 struct berval *entryUUID,
490 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
492 #ifdef BACKSQL_ARBITRARY_KEY
495 #endif /* BACKSQL_ARBITRARY_KEY */
497 /* entryUUID is generated as "%08x-%04x-%04x-0000-eaddrXXX"
498 * with eid_oc_id as %08x and hi and lo eid_id as %04x-%04x */
499 assert( bi != NULL );
500 assert( id != NULL );
501 assert( entryUUID != NULL );
503 #ifdef BACKSQL_ARBITRARY_KEY
504 snprintf( uuidbuf, sizeof( uuidbuf ),
505 "%08x-0000-0000-0000-000000000000",
506 ( id->eid_oc_id & 0xFFFFFFFF ) );
507 lmax = id->eid_keyval.bv_len < 12 ? id->eid_keyval.bv_len : 12;
508 for ( l = 0, i = 9; l < lmax; l++, i += 2 ) {
510 case STRLENOF( "00000000-0000" ):
511 case STRLENOF( "00000000-0000-0000" ):
512 case STRLENOF( "00000000-0000-0000-0000" ):
513 uuidbuf[ i++ ] = '-';
517 snprintf( &uuidbuf[ i ], 3, "%2x", id->eid_keyval.bv_val[ l ] );
521 #else /* ! BACKSQL_ARBITRARY_KEY */
522 /* note: works only with 32 bit architectures... */
523 snprintf( uuidbuf, sizeof( uuidbuf ),
524 "%08x-%04x-%04x-0000-000000000000",
525 ( (unsigned)id->eid_oc_id & 0xFFFFFFFF ),
526 ( ( (unsigned)id->eid_keyval & 0xFFFF0000 ) >> 020 /* 16 */ ),
527 ( (unsigned)id->eid_keyval & 0xFFFF ) );
528 #endif /* ! BACKSQL_ARBITRARY_KEY */
530 uuid.bv_val = uuidbuf;
531 uuid.bv_len = strlen( uuidbuf );
533 ber_dupbv_x( entryUUID, &uuid, memctx );
539 backsql_entryUUID_decode(
540 struct berval *entryUUID,
541 unsigned long *oc_id,
542 #ifdef BACKSQL_ARBITRARY_KEY
543 struct berval *keyval
544 #else /* ! BACKSQL_ARBITRARY_KEY */
545 unsigned long *keyval
546 #endif /* ! BACKSQL_ARBITRARY_KEY */
550 fprintf( stderr, "==> backsql_entryUUID_decode()\n" );
553 *oc_id = ( entryUUID->bv_val[0] << 030 /* 24 */ )
554 + ( entryUUID->bv_val[1] << 020 /* 16 */ )
555 + ( entryUUID->bv_val[2] << 010 /* 8 */ )
556 + entryUUID->bv_val[3];
558 #ifdef BACKSQL_ARBITRARY_KEY
560 #else /* ! BACKSQL_ARBITRARY_KEY */
561 *keyval = ( entryUUID->bv_val[4] << 030 /* 24 */ )
562 + ( entryUUID->bv_val[5] << 020 /* 16 */ )
563 + ( entryUUID->bv_val[6] << 010 /* 8 */ )
564 + entryUUID->bv_val[7];
565 #endif /* ! BACKSQL_ARBITRARY_KEY */
568 fprintf( stderr, "<== backsql_entryUUID_decode(): oc=%lu id=%lu\n",