Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / openldap / dist / servers / slapd / back-sql / util.c
blob88ae7a275e05b895c84881998e4f1ccbf9b6eb3f
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.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
11 * Public License.
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>.
17 /* ACKNOWLEDGEMENTS:
18 * This work was initially developed by Dmitry Kovalev for inclusion
19 * by OpenLDAP Software. Additional significant contributors include
20 * Pierangelo Masarati.
23 #include "portable.h"
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28 #include "ac/ctype.h"
29 #include "ac/stdarg.h"
31 #include "slap.h"
32 #include "proto-sql.h"
33 #include "lutil.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 "
49 "WHERE oc_map_id=?";
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) "
55 "VALUES (?,?,?,?)";
56 const char backsql_def_delobjclasses_stmt[] = "DELETE FROM ldap_entry_objclasses "
57 "WHERE entry_id=?";
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(?,?)";
64 /* TimesTen */
65 const char backsql_check_dn_ru_query[] = "SELECT dn_ru FROM ldap_entries";
67 struct berbuf *
68 backsql_strcat_x( struct berbuf *dest, void *memctx, ... )
70 va_list strs;
71 ber_len_t cdlen, cslen, grow;
72 char *cstr;
74 assert( dest != NULL );
75 assert( dest->bb_val.bv_val == NULL
76 || dest->bb_val.bv_len == strlen( dest->bb_val.bv_val ) );
78 #ifdef BACKSQL_TRACE
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 ) {
93 char *tmp_dest;
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 */
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",
107 0, 0, 0 );
108 return NULL;
110 dest->bb_val.bv_val = tmp_dest;
111 dest->bb_len += grow;
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 */
119 AC_MEMCPY( dest->bb_val.bv_val + cdlen, cstr, cslen + 1 );
120 cdlen += cslen;
122 va_end( strs );
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 */
129 dest->bb_val.bv_len = cdlen;
131 return dest;
134 struct berbuf *
135 backsql_strfcat_x( struct berbuf *dest, void *memctx, const char *fmt, ... )
137 va_list strs;
138 ber_len_t cdlen;
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 ) );
146 #ifdef BACKSQL_TRACE
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' };
161 struct berval *cbv;
163 switch ( fmt[ 0 ] ) {
165 /* berval */
166 case 'b':
167 cbv = va_arg( strs, struct berval * );
168 cstr = cbv->bv_val;
169 cslen = cbv->bv_len;
170 break;
172 /* length + string */
173 case 'l':
174 cslen = va_arg( strs, ber_len_t );
175 cstr = va_arg( strs, char * );
176 break;
178 /* string */
179 case 's':
180 cstr = va_arg( strs, char * );
181 cslen = strlen( cstr );
182 break;
184 /* char */
185 case 'c':
187 * `char' is promoted to `int' when passed through `...'
189 cc[0] = va_arg( strs, int );
190 cstr = cc;
191 cslen = 1;
192 break;
194 default:
195 assert( 0 );
198 grow = BACKSQL_MAX( BACKSQL_STR_GROW, cslen );
199 if ( dest->bb_len - cdlen <= cslen ) {
200 char *tmp_dest;
202 #ifdef BACKSQL_TRACE
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",
214 0, 0, 0 );
215 return NULL;
217 dest->bb_val.bv_val = tmp_dest;
218 dest->bb_len += grow * sizeof( char );
220 #ifdef BACKSQL_TRACE
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 );
229 cdlen += cslen;
232 va_end( strs );
234 #ifdef BACKSQL_TRACE
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;
241 return dest;
245 backsql_entry_addattr(
246 Entry *e,
247 AttributeDescription *ad,
248 struct berval *val,
249 void *memctx )
251 int rc;
253 #ifdef BACKSQL_TRACE
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 );
264 return rc;
267 #ifdef BACKSQL_TRACE
268 Debug( LDAP_DEBUG_TRACE, "<==backsql_entry_addattr(\"%s\")\n",
269 e->e_name.bv_val, 0, 0 );
270 #endif /* BACKSQL_TRACE */
272 return LDAP_SUCCESS;
275 static char *
276 backsql_get_table_spec( backsql_info *bi, char **p )
278 char *s, *q;
279 struct berbuf res = BB_NULL;
281 assert( p != NULL );
282 assert( *p != NULL );
284 s = *p;
285 while ( **p && **p != ',' ) {
286 (*p)++;
289 if ( **p ) {
290 *(*p)++ = '\0';
293 #define BACKSQL_NEXT_WORD { \
294 while ( *s && isspace( (unsigned char)*s ) ) s++; \
295 if ( !*s ) return res.bb_val.bv_val; \
296 q = s; \
297 while ( *q && !isspace( (unsigned char)*q ) ) q++; \
298 if ( *q ) *q++='\0'; \
301 BACKSQL_NEXT_WORD;
302 /* table name */
303 backsql_strcat_x( &res, NULL, s, NULL );
304 s = q;
306 BACKSQL_NEXT_WORD;
307 if ( strcasecmp( s, "AS" ) == 0 ) {
308 s = q;
309 BACKSQL_NEXT_WORD;
312 /* oracle doesn't understand "AS" :( and other RDBMSes don't need it */
313 backsql_strfcat_x( &res, NULL, "lbbsb",
314 STRLENOF( " " ), " ",
315 &bi->sql_aliasing,
316 &bi->sql_aliasing_quote,
318 &bi->sql_aliasing_quote );
320 return res.bb_val.bv_val;
324 backsql_merge_from_clause(
325 backsql_info *bi,
326 struct berbuf *dest_from,
327 struct berval *src_from )
329 char *s, *p, *srcc, *pos, e;
330 struct berbuf res = BB_NULL;
332 #ifdef BACKSQL_TRACE
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 );
340 p = srcc;
342 if ( dest_from != NULL ) {
343 res = *dest_from;
346 while ( *p ) {
347 s = backsql_get_table_spec( bi, &p );
349 #ifdef BACKSQL_TRACE
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 );
357 } else {
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 );
364 if ( s ) {
365 ch_free( s );
369 #ifdef BACKSQL_TRACE
370 Debug( LDAP_DEBUG_TRACE, "<==backsql_merge_from_clause()\n", 0, 0, 0 );
371 #endif /* BACKSQL_TRACE */
373 free( srcc );
374 *dest_from = res;
376 return 1;
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,
390 int expected )
392 char *pattern, *start, *end;
393 struct berval bv;
394 int rc = 0;
396 #define SPLIT_CHAR '?'
398 assert( _pattern != NULL );
399 assert( split_pattern != NULL );
401 pattern = ch_strdup( _pattern );
403 start = pattern;
404 end = strchr( start, SPLIT_CHAR );
405 for ( ; start; expected-- ) {
406 char *real_end = end;
407 ber_len_t real_len;
409 if ( real_end == NULL ) {
410 real_end = start + strlen( start );
412 } else if ( real_end[ 1 ] == SPLIT_CHAR ) {
413 expected++;
414 AC_MEMCPY( real_end, real_end + 1, strlen( real_end ) );
415 end = strchr( real_end + 1, SPLIT_CHAR );
416 continue;
419 real_len = real_end - start;
420 if ( real_len == 0 ) {
421 ber_str2bv( "", 0, 1, &bv );
422 } else {
423 ber_str2bv( start, real_len, 1, &bv );
426 ber_bvarray_add( split_pattern, &bv );
428 if ( expected == 0 ) {
429 if ( end != NULL ) {
430 rc = -1;
431 goto done;
433 break;
436 if ( end != NULL ) {
437 start = end + 1;
438 end = strchr( start, SPLIT_CHAR );
442 done:;
444 ch_free( pattern );
446 return rc;
450 backsql_prepare_pattern(
451 BerVarray split_pattern,
452 BerVarray values,
453 struct berval *res )
455 int i;
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 );
463 return -1;
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 );
471 return -1;
474 backsql_strfcat_x( &bb, NULL, "b", &split_pattern[ i ] );
476 *res = bb.bb_val;
478 return 0;
482 backsql_entryUUID(
483 backsql_info *bi,
484 backsql_entryID *id,
485 struct berval *entryUUID,
486 void *memctx )
488 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
489 struct berval uuid;
490 #ifdef BACKSQL_ARBITRARY_KEY
491 int i;
492 ber_len_t l, lmax;
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 ) {
507 switch ( i ) {
508 case STRLENOF( "00000000-0000" ):
509 case STRLENOF( "00000000-0000-0000" ):
510 case STRLENOF( "00000000-0000-0000-0000" ):
511 uuidbuf[ i++ ] = '-';
512 /* FALLTHRU */
514 default:
515 snprintf( &uuidbuf[ i ], 3, "%2x", id->eid_keyval.bv_val[ l ] );
516 break;
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 );
533 return 0;
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 */
547 #if 0
548 fprintf( stderr, "==> backsql_entryUUID_decode()\n" );
549 #endif
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
557 /* FIXME */
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 */
565 #if 0
566 fprintf( stderr, "<== backsql_entryUUID_decode(): oc=%lu id=%lu\n",
567 *oc_id, *keyval );
568 #endif
570 return LDAP_SUCCESS;