1 /* $OpenLDAP: pkg/ldap/servers/slapd/back-sql/util.c,v 1.45.2.4 2008/02/11 23:26:48 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2008 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",
110 dest
->bb_val
.bv_val
= tmp_dest
;
111 dest
->bb_len
+= grow
;
114 Debug( LDAP_DEBUG_TRACE
, "backsql_strcat(): "
115 "new buflen=%d, dest=%p\n",
116 dest
->bb_len
, dest
, 0 );
117 #endif /* BACKSQL_TRACE */
119 AC_MEMCPY( dest
->bb_val
.bv_val
+ cdlen
, cstr
, cslen
+ 1 );
125 Debug( LDAP_DEBUG_TRACE
, "<==backsql_strcat() (dest=\"%s\")\n",
126 dest
->bb_val
.bv_val
, 0, 0 );
127 #endif /* BACKSQL_TRACE */
129 dest
->bb_val
.bv_len
= cdlen
;
135 backsql_strfcat_x( struct berbuf
*dest
, void *memctx
, const char *fmt
, ... )
140 assert( dest
!= NULL
);
141 assert( fmt
!= NULL
);
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
) );
147 Debug( LDAP_DEBUG_TRACE
, "==>backsql_strfcat()\n", 0, 0, 0 );
148 #endif /* BACKSQL_TRACE */
150 va_start( strs
, fmt
);
151 if ( dest
->bb_val
.bv_val
== NULL
|| dest
->bb_len
== 0 ) {
152 dest
->bb_val
.bv_val
= (char *)ber_memalloc_x( BACKSQL_STR_GROW
* sizeof( char ), memctx
);
153 dest
->bb_val
.bv_len
= 0;
154 dest
->bb_len
= BACKSQL_STR_GROW
;
157 cdlen
= dest
->bb_val
.bv_len
;
158 for ( ; fmt
[0]; fmt
++ ) {
159 ber_len_t cslen
, grow
;
160 char *cstr
, cc
[ 2 ] = { '\0', '\0' };
163 switch ( fmt
[ 0 ] ) {
167 cbv
= va_arg( strs
, struct berval
* );
172 /* length + string */
174 cslen
= va_arg( strs
, ber_len_t
);
175 cstr
= va_arg( strs
, char * );
180 cstr
= va_arg( strs
, char * );
181 cslen
= strlen( cstr
);
187 * `char' is promoted to `int' when passed through `...'
189 cc
[0] = va_arg( strs
, int );
198 grow
= BACKSQL_MAX( BACKSQL_STR_GROW
, cslen
);
199 if ( dest
->bb_len
- cdlen
<= cslen
) {
203 Debug( LDAP_DEBUG_TRACE
, "backsql_strfcat(): "
204 "buflen=%d, cdlen=%d, cslen=%d "
205 "-- reallocating dest\n",
206 dest
->bb_len
, cdlen
+ 1, cslen
);
207 #endif /* BACKSQL_TRACE */
209 tmp_dest
= (char *)ber_memrealloc_x( dest
->bb_val
.bv_val
,
210 ( dest
->bb_len
) + grow
* sizeof( char ), memctx
);
211 if ( tmp_dest
== NULL
) {
212 Debug( LDAP_DEBUG_ANY
, "backsql_strfcat(): "
213 "could not reallocate string buffer.\n",
217 dest
->bb_val
.bv_val
= tmp_dest
;
218 dest
->bb_len
+= grow
* sizeof( char );
221 Debug( LDAP_DEBUG_TRACE
, "backsql_strfcat(): "
222 "new buflen=%d, dest=%p\n", dest
->bb_len
, dest
, 0 );
223 #endif /* BACKSQL_TRACE */
226 assert( cstr
!= NULL
);
228 AC_MEMCPY( dest
->bb_val
.bv_val
+ cdlen
, cstr
, cslen
+ 1 );
235 Debug( LDAP_DEBUG_TRACE
, "<==backsql_strfcat() (dest=\"%s\")\n",
236 dest
->bb_val
.bv_val
, 0, 0 );
237 #endif /* BACKSQL_TRACE */
239 dest
->bb_val
.bv_len
= cdlen
;
245 backsql_entry_addattr(
247 AttributeDescription
*ad
,
254 Debug( LDAP_DEBUG_TRACE
, "backsql_entry_addattr(\"%s\"): %s=%s\n",
255 e
->e_name
.bv_val
, ad
->ad_cname
.bv_val
, val
->bv_val
);
256 #endif /* BACKSQL_TRACE */
258 rc
= attr_merge_normalize_one( e
, ad
, val
, memctx
);
260 if ( rc
!= LDAP_SUCCESS
) {
261 Debug( LDAP_DEBUG_TRACE
, "backsql_entry_addattr(\"%s\"): "
262 "failed to merge value \"%s\" for attribute \"%s\"\n",
263 e
->e_name
.bv_val
, val
->bv_val
, ad
->ad_cname
.bv_val
);
268 Debug( LDAP_DEBUG_TRACE
, "<==backsql_entry_addattr(\"%s\")\n",
269 e
->e_name
.bv_val
, 0, 0 );
270 #endif /* BACKSQL_TRACE */
276 backsql_get_table_spec( backsql_info
*bi
, char **p
)
279 struct berbuf res
= BB_NULL
;
282 assert( *p
!= NULL
);
285 while ( **p
&& **p
!= ',' ) {
293 #define BACKSQL_NEXT_WORD { \
294 while ( *s && isspace( (unsigned char)*s ) ) s++; \
295 if ( !*s ) return res.bb_val.bv_val; \
297 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
298 if ( *q ) *q++='\0'; \
303 backsql_strcat_x( &res
, NULL
, s
, NULL
);
307 if ( strcasecmp( s
, "AS" ) == 0 ) {
312 /* oracle doesn't understand "AS" :( and other RDBMSes don't need it */
313 backsql_strfcat_x( &res
, NULL
, "lbbsb",
314 STRLENOF( " " ), " ",
316 &bi
->sql_aliasing_quote
,
318 &bi
->sql_aliasing_quote
);
320 return res
.bb_val
.bv_val
;
324 backsql_merge_from_clause(
326 struct berbuf
*dest_from
,
327 struct berval
*src_from
)
329 char *s
, *p
, *srcc
, *pos
, e
;
330 struct berbuf res
= BB_NULL
;
333 Debug( LDAP_DEBUG_TRACE
, "==>backsql_merge_from_clause(): "
334 "dest_from=\"%s\",src_from=\"%s\"\n",
335 dest_from
? dest_from
->bb_val
.bv_val
: "<NULL>",
336 src_from
->bv_val
, 0 );
337 #endif /* BACKSQL_TRACE */
339 srcc
= ch_strdup( src_from
->bv_val
);
342 if ( dest_from
!= NULL
) {
347 s
= backsql_get_table_spec( bi
, &p
);
350 Debug( LDAP_DEBUG_TRACE
, "backsql_merge_from_clause(): "
351 "p=\"%s\" s=\"%s\"\n", p
, s
, 0 );
352 #endif /* BACKSQL_TRACE */
354 if ( BER_BVISNULL( &res
.bb_val
) ) {
355 backsql_strcat_x( &res
, NULL
, s
, NULL
);
358 pos
= strstr( res
.bb_val
.bv_val
, s
);
359 if ( pos
== NULL
|| ( ( e
= pos
[ strlen( s
) ] ) != '\0' && e
!= ',' ) ) {
360 backsql_strfcat_x( &res
, NULL
, "cs", ',', s
);
370 Debug( LDAP_DEBUG_TRACE
, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
371 #endif /* BACKSQL_TRACE */
380 * splits a pattern in components separated by '?'
381 * (double ?? are turned into single ? and left in the string)
382 * expected contains the number of expected occurrences of '?'
383 * (a negative value means parse as many as possible)
387 backsql_split_pattern(
388 const char *_pattern
,
389 BerVarray
*split_pattern
,
392 char *pattern
, *start
, *end
;
396 #define SPLIT_CHAR '?'
398 assert( _pattern
!= NULL
);
399 assert( split_pattern
!= NULL
);
401 pattern
= ch_strdup( _pattern
);
404 end
= strchr( start
, SPLIT_CHAR
);
405 for ( ; start
; expected
-- ) {
406 char *real_end
= end
;
409 if ( real_end
== NULL
) {
410 real_end
= start
+ strlen( start
);
412 } else if ( real_end
[ 1 ] == SPLIT_CHAR
) {
414 AC_MEMCPY( real_end
, real_end
+ 1, strlen( real_end
) );
415 end
= strchr( real_end
+ 1, SPLIT_CHAR
);
419 real_len
= real_end
- start
;
420 if ( real_len
== 0 ) {
421 ber_str2bv( "", 0, 1, &bv
);
423 ber_str2bv( start
, real_len
, 1, &bv
);
426 ber_bvarray_add( split_pattern
, &bv
);
428 if ( expected
== 0 ) {
438 end
= strchr( start
, SPLIT_CHAR
);
450 backsql_prepare_pattern(
451 BerVarray split_pattern
,
456 struct berbuf bb
= BB_NULL
;
458 assert( res
!= NULL
);
460 for ( i
= 0; values
[i
].bv_val
; i
++ ) {
461 if ( split_pattern
[i
].bv_val
== NULL
) {
462 ch_free( bb
.bb_val
.bv_val
);
465 backsql_strfcat_x( &bb
, NULL
, "b", &split_pattern
[ i
] );
466 backsql_strfcat_x( &bb
, NULL
, "b", &values
[ i
] );
469 if ( split_pattern
[ i
].bv_val
== NULL
) {
470 ch_free( bb
.bb_val
.bv_val
);
474 backsql_strfcat_x( &bb
, NULL
, "b", &split_pattern
[ i
] );
485 struct berval
*entryUUID
,
488 char uuidbuf
[ LDAP_LUTIL_UUIDSTR_BUFSIZE
];
490 #ifdef BACKSQL_ARBITRARY_KEY
493 #endif /* BACKSQL_ARBITRARY_KEY */
495 /* entryUUID is generated as "%08x-%04x-%04x-0000-eaddrXXX"
496 * with eid_oc_id as %08x and hi and lo eid_id as %04x-%04x */
497 assert( bi
!= NULL
);
498 assert( id
!= NULL
);
499 assert( entryUUID
!= NULL
);
501 #ifdef BACKSQL_ARBITRARY_KEY
502 snprintf( uuidbuf
, sizeof( uuidbuf
),
503 "%08x-0000-0000-0000-000000000000",
504 ( id
->eid_oc_id
& 0xFFFFFFFF ) );
505 lmax
= id
->eid_keyval
.bv_len
< 12 ? id
->eid_keyval
.bv_len
: 12;
506 for ( l
= 0, i
= 9; l
< lmax
; l
++, i
+= 2 ) {
508 case STRLENOF( "00000000-0000" ):
509 case STRLENOF( "00000000-0000-0000" ):
510 case STRLENOF( "00000000-0000-0000-0000" ):
511 uuidbuf
[ i
++ ] = '-';
515 snprintf( &uuidbuf
[ i
], 3, "%2x", id
->eid_keyval
.bv_val
[ l
] );
519 #else /* ! BACKSQL_ARBITRARY_KEY */
520 /* note: works only with 32 bit architectures... */
521 snprintf( uuidbuf
, sizeof( uuidbuf
),
522 "%08x-%04x-%04x-0000-000000000000",
523 ( (unsigned)id
->eid_oc_id
& 0xFFFFFFFF ),
524 ( ( (unsigned)id
->eid_keyval
& 0xFFFF0000 ) >> 020 /* 16 */ ),
525 ( (unsigned)id
->eid_keyval
& 0xFFFF ) );
526 #endif /* ! BACKSQL_ARBITRARY_KEY */
528 uuid
.bv_val
= uuidbuf
;
529 uuid
.bv_len
= strlen( uuidbuf
);
531 ber_dupbv_x( entryUUID
, &uuid
, memctx
);
537 backsql_entryUUID_decode(
538 struct berval
*entryUUID
,
539 unsigned long *oc_id
,
540 #ifdef BACKSQL_ARBITRARY_KEY
541 struct berval
*keyval
542 #else /* ! BACKSQL_ARBITRARY_KEY */
543 unsigned long *keyval
544 #endif /* ! BACKSQL_ARBITRARY_KEY */
548 fprintf( stderr
, "==> backsql_entryUUID_decode()\n" );
551 *oc_id
= ( entryUUID
->bv_val
[0] << 030 /* 24 */ )
552 + ( entryUUID
->bv_val
[1] << 020 /* 16 */ )
553 + ( entryUUID
->bv_val
[2] << 010 /* 8 */ )
554 + entryUUID
->bv_val
[3];
556 #ifdef BACKSQL_ARBITRARY_KEY
558 #else /* ! BACKSQL_ARBITRARY_KEY */
559 *keyval
= ( entryUUID
->bv_val
[4] << 030 /* 24 */ )
560 + ( entryUUID
->bv_val
[5] << 020 /* 16 */ )
561 + ( entryUUID
->bv_val
[6] << 010 /* 8 */ )
562 + entryUUID
->bv_val
[7];
563 #endif /* ! BACKSQL_ARBITRARY_KEY */
566 fprintf( stderr
, "<== backsql_entryUUID_decode(): oc=%lu id=%lu\n",