4 Copyright (C) Derrell Lipman 2005
5 Copyright (C) Simo Sorce 2005
7 ** NOTE! The following LGPL license applies to the ldb
8 ** library. This does NOT imply that all of Samba is released
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Component: ldb sqlite3 backend
31 * Description: core files for SQLITE3 backend
33 * Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
37 #include "ldb/include/includes.h"
41 struct lsqlite3_private
{
47 struct lsql_async_context
{
48 struct ldb_module
*module
;
51 long long current_eid
;
52 const char * const * attrs
;
53 struct ldb_async_result
*ares
;
57 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*);
60 static struct ldb_async_handle
*init_handle(struct lsqlite3_private
*lsqlite3
, struct ldb_module
*module
,
62 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*))
64 struct lsql_async_context
*ac
;
65 struct ldb_async_handle
*h
;
67 h
= talloc_zero(lsqlite3
, struct ldb_async_handle
);
69 ldb_set_errstring(module
->ldb
, talloc_asprintf(module
, "Out of Memory"));
75 ac
= talloc(h
, struct lsql_async_context
);
77 ldb_set_errstring(module
->ldb
, talloc_asprintf(module
, "Out of Memory"));
82 h
->private_data
= (void *)ac
;
84 h
->state
= LDB_ASYNC_INIT
;
85 h
->status
= LDB_SUCCESS
;
88 ac
->context
= context
;
89 ac
->callback
= callback
;
95 * Macros used throughout
100 # define TRUE (! FALSE)
103 #define RESULT_ATTR_TABLE "temp_result_attrs"
105 //#define TEMPTAB /* for testing, create non-temporary table */
106 #define TEMPTAB "TEMPORARY"
111 sqlite3_stmt
* stmtGetEID
= NULL
;
113 static char *lsqlite3_tprintf(TALLOC_CTX
*mem_ctx
, const char *fmt
, ...)
119 str
= sqlite3_vmprintf(fmt
, ap
);
122 if (str
== NULL
) return NULL
;
124 ret
= talloc_strdup(mem_ctx
, str
);
134 static char base160tab
[161] = {
135 48 ,49 ,50 ,51 ,52 ,53 ,54 ,55 ,56 ,57 , /* 0-9 */
136 58 ,59 ,65 ,66 ,67 ,68 ,69 ,70 ,71 ,72 , /* : ; A-H */
137 73 ,74 ,75 ,76 ,77 ,78 ,79 ,80 ,81 ,82 , /* I-R */
138 83 ,84 ,85 ,86 ,87 ,88 ,89 ,90 ,97 ,98 , /* S-Z , a-b */
139 99 ,100,101,102,103,104,105,106,107,108, /* c-l */
140 109,110,111,112,113,114,115,116,117,118, /* m-v */
141 119,120,121,122,160,161,162,163,164,165, /* w-z, latin1 */
142 166,167,168,169,170,171,172,173,174,175, /* latin1 */
143 176,177,178,179,180,181,182,183,184,185, /* latin1 */
144 186,187,188,189,190,191,192,193,194,195, /* latin1 */
145 196,197,198,199,200,201,202,203,204,205, /* latin1 */
146 206,207,208,209,210,211,212,213,214,215, /* latin1 */
147 216,217,218,219,220,221,222,223,224,225, /* latin1 */
148 226,227,228,229,230,231,232,233,234,235, /* latin1 */
149 236,237,238,239,240,241,242,243,244,245, /* latin1 */
150 246,247,248,249,250,251,252,253,254,255, /* latin1 */
158 * Convert an unsigned long integer into a base160 representation of the
163 * value to be converted
166 * character array, 5 bytes long, into which the base160 representation
167 * will be placed. The result will be a four-digit representation of the
168 * number (with leading zeros prepended as necessary), and null
175 base160_sql(sqlite3_context
* hContext
,
177 sqlite3_value
** argv
)
183 val
= sqlite3_value_int64(argv
[0]);
185 for (i
= 3; i
>= 0; i
--) {
187 result
[i
] = base160tab
[val
% 160];
193 sqlite3_result_text(hContext
, result
, -1, SQLITE_TRANSIENT
);
200 * This function enhances sqlite by adding a "base160_next()" function which is
201 * accessible via queries.
203 * Retrieve the next-greater number in the base160 sequence for the terminal
204 * tree node (the last four digits). Only one tree level (four digits) is
208 * A character string: either an empty string (in which case no operation is
209 * performed), or a string of base160 digits with a length of a multiple of
213 * Upon return, the trailing four digits (one tree level) will have been
217 base160next_sql(sqlite3_context
* hContext
,
219 sqlite3_value
** argv
)
224 char * pBase160
= strdup((const char *)sqlite3_value_text(argv
[0]));
225 char * pStart
= pBase160
;
228 * We need a minimum of four digits, and we will always get a multiple
231 if (pBase160
!= NULL
&&
232 (len
= strlen(pBase160
)) >= 4 &&
235 if (pBase160
== NULL
) {
237 sqlite3_result_null(hContext
);
241 pBase160
+= strlen(pBase160
) - 1;
243 /* We only carry through four digits: one level in the tree */
244 for (i
= 0; i
< 4; i
++) {
246 /* What base160 value does this digit have? */
247 pTab
= strchr(base160tab
, *pBase160
);
249 /* Is there a carry? */
250 if (pTab
< base160tab
+ sizeof(base160tab
) - 1) {
253 * Nope. Just increment this value and we're
261 * There's a carry. This value gets
262 * base160tab[0], we decrement the buffer
263 * pointer to get the next higher-order digit,
264 * and continue in the loop.
266 *pBase160
-- = base160tab
[0];
270 sqlite3_result_text(hContext
,
275 sqlite3_result_value(hContext
, argv
[0]);
276 if (pBase160
!= NULL
) {
282 static char *parsetree_to_sql(struct ldb_module
*module
,
284 const struct ldb_parse_tree
*t
)
286 const struct ldb_attrib_handler
*h
;
287 struct ldb_val value
, subval
;
288 char *wild_card_string
;
295 switch(t
->operation
) {
298 tmp
= parsetree_to_sql(module
, mem_ctx
, t
->u
.list
.elements
[0]);
299 if (tmp
== NULL
) return NULL
;
301 for (i
= 1; i
< t
->u
.list
.num_elements
; i
++) {
303 child
= parsetree_to_sql(module
, mem_ctx
, t
->u
.list
.elements
[i
]);
304 if (child
== NULL
) return NULL
;
306 tmp
= talloc_asprintf_append(tmp
, " INTERSECT %s ", child
);
307 if (tmp
== NULL
) return NULL
;
310 ret
= talloc_asprintf(mem_ctx
, "SELECT * FROM ( %s )\n", tmp
);
316 tmp
= parsetree_to_sql(module
, mem_ctx
, t
->u
.list
.elements
[0]);
317 if (tmp
== NULL
) return NULL
;
319 for (i
= 1; i
< t
->u
.list
.num_elements
; i
++) {
321 child
= parsetree_to_sql(module
, mem_ctx
, t
->u
.list
.elements
[i
]);
322 if (child
== NULL
) return NULL
;
324 tmp
= talloc_asprintf_append(tmp
, " UNION %s ", child
);
325 if (tmp
== NULL
) return NULL
;
328 return talloc_asprintf(mem_ctx
, "SELECT * FROM ( %s ) ", tmp
);
332 child
= parsetree_to_sql(module
, mem_ctx
, t
->u
.isnot
.child
);
333 if (child
== NULL
) return NULL
;
335 return talloc_asprintf(mem_ctx
,
336 "SELECT eid FROM ldb_entry "
337 "WHERE eid NOT IN ( %s ) ", child
);
339 case LDB_OP_EQUALITY
:
341 * For simple searches, we want to retrieve the list of EIDs that
342 * match the criteria.
344 attr
= ldb_attr_casefold(mem_ctx
, t
->u
.equality
.attr
);
345 if (attr
== NULL
) return NULL
;
346 h
= ldb_attrib_handler(module
->ldb
, attr
);
348 /* Get a canonicalised copy of the data */
349 h
->canonicalise_fn(module
->ldb
, mem_ctx
, &(t
->u
.equality
.value
), &value
);
350 if (value
.data
== NULL
) {
354 if (strcasecmp(t
->u
.equality
.attr
, "objectclass") == 0) {
356 * For object classes, we want to search for all objectclasses
357 * that are subclasses as well.
359 return lsqlite3_tprintf(mem_ctx
,
360 "SELECT eid FROM ldb_attribute_values\n"
361 "WHERE norm_attr_name = 'OBJECTCLASS' "
362 "AND norm_attr_value IN\n"
363 " (SELECT class_name FROM ldb_object_classes\n"
364 " WHERE tree_key GLOB\n"
365 " (SELECT tree_key FROM ldb_object_classes\n"
366 " WHERE class_name = '%q'\n"
370 } else if (strcasecmp(t
->u
.equality
.attr
, "dn") == 0) {
371 /* DN query is a special ldb case */
372 char *cdn
= ldb_dn_linearize_casefold(module
->ldb
,
373 ldb_dn_explode(module
->ldb
,
374 (const char *)value
.data
));
376 return lsqlite3_tprintf(mem_ctx
,
377 "SELECT eid FROM ldb_entry "
378 "WHERE norm_dn = '%q'", cdn
);
381 /* A normal query. */
382 return lsqlite3_tprintf(mem_ctx
,
383 "SELECT eid FROM ldb_attribute_values "
384 "WHERE norm_attr_name = '%q' "
385 "AND norm_attr_value = '%q'",
391 case LDB_OP_SUBSTRING
:
393 wild_card_string
= talloc_strdup(mem_ctx
,
394 (t
->u
.substring
.start_with_wildcard
)?"*":"");
395 if (wild_card_string
== NULL
) return NULL
;
397 for (i
= 0; t
->u
.substring
.chunks
[i
]; i
++) {
398 wild_card_string
= talloc_asprintf_append(wild_card_string
, "%s*",
399 t
->u
.substring
.chunks
[i
]->data
);
400 if (wild_card_string
== NULL
) return NULL
;
403 if ( ! t
->u
.substring
.end_with_wildcard
) {
404 /* remove last wildcard */
405 wild_card_string
[strlen(wild_card_string
) - 1] = '\0';
408 attr
= ldb_attr_casefold(mem_ctx
, t
->u
.substring
.attr
);
409 if (attr
== NULL
) return NULL
;
410 h
= ldb_attrib_handler(module
->ldb
, attr
);
412 subval
.data
= (void *)wild_card_string
;
413 subval
.length
= strlen(wild_card_string
) + 1;
415 /* Get a canonicalised copy of the data */
416 h
->canonicalise_fn(module
->ldb
, mem_ctx
, &(subval
), &value
);
417 if (value
.data
== NULL
) {
421 return lsqlite3_tprintf(mem_ctx
,
422 "SELECT eid FROM ldb_attribute_values "
423 "WHERE norm_attr_name = '%q' "
424 "AND norm_attr_value GLOB '%q'",
429 attr
= ldb_attr_casefold(mem_ctx
, t
->u
.equality
.attr
);
430 if (attr
== NULL
) return NULL
;
431 h
= ldb_attrib_handler(module
->ldb
, attr
);
433 /* Get a canonicalised copy of the data */
434 h
->canonicalise_fn(module
->ldb
, mem_ctx
, &(t
->u
.equality
.value
), &value
);
435 if (value
.data
== NULL
) {
439 return lsqlite3_tprintf(mem_ctx
,
440 "SELECT eid FROM ldb_attribute_values "
441 "WHERE norm_attr_name = '%q' "
442 "AND ldap_compare(norm_attr_value, '>=', '%q', '%q') ",
448 attr
= ldb_attr_casefold(mem_ctx
, t
->u
.equality
.attr
);
449 if (attr
== NULL
) return NULL
;
450 h
= ldb_attrib_handler(module
->ldb
, attr
);
452 /* Get a canonicalised copy of the data */
453 h
->canonicalise_fn(module
->ldb
, mem_ctx
, &(t
->u
.equality
.value
), &value
);
454 if (value
.data
== NULL
) {
458 return lsqlite3_tprintf(mem_ctx
,
459 "SELECT eid FROM ldb_attribute_values "
460 "WHERE norm_attr_name = '%q' "
461 "AND ldap_compare(norm_attr_value, '<=', '%q', '%q') ",
467 if (strcasecmp(t
->u
.present
.attr
, "dn") == 0) {
468 return talloc_strdup(mem_ctx
, "SELECT eid FROM ldb_entry");
471 attr
= ldb_attr_casefold(mem_ctx
, t
->u
.present
.attr
);
472 if (attr
== NULL
) return NULL
;
474 return lsqlite3_tprintf(mem_ctx
,
475 "SELECT eid FROM ldb_attribute_values "
476 "WHERE norm_attr_name = '%q' ",
480 attr
= ldb_attr_casefold(mem_ctx
, t
->u
.equality
.attr
);
481 if (attr
== NULL
) return NULL
;
482 h
= ldb_attrib_handler(module
->ldb
, attr
);
484 /* Get a canonicalised copy of the data */
485 h
->canonicalise_fn(module
->ldb
, mem_ctx
, &(t
->u
.equality
.value
), &value
);
486 if (value
.data
== NULL
) {
490 return lsqlite3_tprintf(mem_ctx
,
491 "SELECT eid FROM ldb_attribute_values "
492 "WHERE norm_attr_name = '%q' "
493 "AND ldap_compare(norm_attr_value, '~%', 'q', '%q') ",
498 case LDB_OP_EXTENDED
:
499 #warning "work out how to handle bitops"
506 /* should never occur */
514 * This function is used for the common case of queries that return a single
517 * NOTE: If more than one value is returned by the query, all but the first
518 * one will be ignored.
521 query_int(const struct lsqlite3_private
* lsqlite3
,
529 sqlite3_stmt
* pStmt
;
532 /* Begin access to variable argument list */
533 va_start(args
, pSql
);
535 /* Format the query */
536 if ((p
= sqlite3_vmprintf(pSql
, args
)) == NULL
) {
541 * Prepare and execute the SQL statement. Loop allows retrying on
542 * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
543 * requiring retrying the operation.
545 for (bLoop
= TRUE
; bLoop
; ) {
547 /* Compile the SQL statement into sqlite virtual machine */
548 if ((ret
= sqlite3_prepare(lsqlite3
->sqlite
,
552 NULL
)) == SQLITE_SCHEMA
) {
553 if (stmtGetEID
!= NULL
) {
554 sqlite3_finalize(stmtGetEID
);
558 } else if (ret
!= SQLITE_OK
) {
562 /* One row expected */
563 if ((ret
= sqlite3_step(pStmt
)) == SQLITE_SCHEMA
) {
564 if (stmtGetEID
!= NULL
) {
565 sqlite3_finalize(stmtGetEID
);
568 (void) sqlite3_finalize(pStmt
);
570 } else if (ret
!= SQLITE_ROW
) {
571 (void) sqlite3_finalize(pStmt
);
575 /* Get the value to be returned */
576 *pRet
= sqlite3_column_int64(pStmt
, 0);
578 /* Free the virtual machine */
579 if ((ret
= sqlite3_finalize(pStmt
)) == SQLITE_SCHEMA
) {
580 if (stmtGetEID
!= NULL
) {
581 sqlite3_finalize(stmtGetEID
);
585 } else if (ret
!= SQLITE_OK
) {
586 (void) sqlite3_finalize(pStmt
);
591 * Normal condition is only one time through loop. Loop is
592 * rerun in error conditions, via "continue", above.
597 /* All done with variable argument list */
601 /* Free the memory we allocated for our query string */
608 * This is a bad hack to support ldap style comparisons whithin sqlite.
609 * val is the attribute in the row currently under test
610 * func is the desired test "<=" ">=" "~" ":"
611 * cmp is the value to compare against (eg: "test")
612 * attr is the attribute name the value of which we want to test
615 static void lsqlite3_compare(sqlite3_context
*ctx
, int argc
,
616 sqlite3_value
**argv
)
618 struct ldb_context
*ldb
= (struct ldb_context
*)sqlite3_user_data(ctx
);
619 const char *val
= (const char *)sqlite3_value_text(argv
[0]);
620 const char *func
= (const char *)sqlite3_value_text(argv
[1]);
621 const char *cmp
= (const char *)sqlite3_value_text(argv
[2]);
622 const char *attr
= (const char *)sqlite3_value_text(argv
[3]);
623 const struct ldb_attrib_handler
*h
;
631 h
= ldb_attrib_handler(ldb
, attr
);
632 valX
.data
= (void *)cmp
;
633 valX
.length
= strlen(cmp
);
634 valY
.data
= (void *)val
;
635 valY
.length
= strlen(val
);
636 ret
= h
->comparison_fn(ldb
, ldb
, &valY
, &valX
);
638 sqlite3_result_int(ctx
, 1);
640 sqlite3_result_int(ctx
, 0);
645 h
= ldb_attrib_handler(ldb
, attr
);
646 valX
.data
= (void *)cmp
;
647 valX
.length
= strlen(cmp
);
648 valY
.data
= (void *)val
;
649 valY
.length
= strlen(val
);
650 ret
= h
->comparison_fn(ldb
, ldb
, &valY
, &valX
);
652 sqlite3_result_int(ctx
, 1);
654 sqlite3_result_int(ctx
, 0);
660 sqlite3_result_int(ctx
, 0);
666 sqlite3_result_int(ctx
, 0);
673 sqlite3_result_error(ctx
, "Value must start with a special operation char (<>~:)!", -1);
678 /* rename a record */
679 static int lsqlite3_safe_rollback(sqlite3
*sqlite
)
685 ret
= sqlite3_exec(sqlite
, "ROLLBACK;", NULL
, NULL
, &errmsg
);
686 if (ret
!= SQLITE_OK
) {
688 printf("lsqlite3_safe_rollback: Error: %s\n", errmsg
);
697 /* return an eid as result */
698 static int lsqlite3_eid_callback(void *result
, int col_num
, char **cols
, char **names
)
700 long long *eid
= (long long *)result
;
702 if (col_num
!= 1) return SQLITE_ABORT
;
703 if (strcasecmp(names
[0], "eid") != 0) return SQLITE_ABORT
;
705 *eid
= atoll(cols
[0]);
710 * add a single set of ldap message values to a ldb_message
712 static int lsqlite3_search_callback(void *result
, int col_num
, char **cols
, char **names
)
714 struct ldb_async_handle
*handle
= talloc_get_type(result
, struct ldb_async_handle
);
715 struct lsql_async_context
*ac
= talloc_get_type(handle
->private_data
, struct lsql_async_context
);
716 struct ldb_message
*msg
;
720 /* eid, dn, attr_name, attr_value */
724 eid
= atoll(cols
[0]);
726 if (eid
!= ac
->current_eid
) { /* here begin a new entry */
728 /* call the async callback for the last entry
729 * except the first time */
730 if (ac
->current_eid
!= 0) {
731 ac
->ares
->message
= ldb_msg_canonicalize(ac
->module
->ldb
, ac
->ares
->message
);
732 if (ac
->ares
->message
== NULL
)
735 handle
->status
= ac
->callback(ac
->module
->ldb
, ac
->context
, ac
->ares
);
736 if (handle
->status
!= LDB_SUCCESS
)
741 ac
->ares
= talloc_zero(ac
, struct ldb_async_result
);
745 ac
->ares
->message
= ldb_msg_new(ac
->ares
);
746 if (!ac
->ares
->message
)
749 ac
->ares
->type
= LDB_REPLY_ENTRY
;
750 ac
->current_eid
= eid
;
753 msg
= ac
->ares
->message
;
755 if (msg
->dn
== NULL
) {
756 msg
->dn
= ldb_dn_explode(msg
, cols
[1]);
763 for (i
= 0; ac
->attrs
[i
]; i
++) {
764 if (strcasecmp(cols
[2], ac
->attrs
[i
]) == 0) {
769 if (!found
) return SQLITE_OK
;
772 if (ldb_msg_add_string(msg
, cols
[2], cols
[3]) != 0) {
782 * lsqlite3_get_eid_ndn()
784 * These functions are used for the very common case of retrieving an EID value
785 * given a (normalized) DN.
788 static long long lsqlite3_get_eid_ndn(sqlite3
*sqlite
, void *mem_ctx
, const char *norm_dn
)
796 query
= lsqlite3_tprintf(mem_ctx
, "SELECT eid "
798 "WHERE norm_dn = '%q';", norm_dn
);
799 if (query
== NULL
) return -1;
801 ret
= sqlite3_exec(sqlite
, query
, lsqlite3_eid_callback
, &eid
, &errmsg
);
802 if (ret
!= SQLITE_OK
) {
804 printf("lsqlite3_get_eid: Fatal Error: %s\n", errmsg
);
813 static long long lsqlite3_get_eid(struct ldb_module
*module
, const struct ldb_dn
*dn
)
815 TALLOC_CTX
*local_ctx
;
816 struct lsqlite3_private
*lsqlite3
= module
->private_data
;
820 /* ignore ltdb specials */
821 if (ldb_dn_is_special(dn
)) {
825 /* create a local ctx */
826 local_ctx
= talloc_named(lsqlite3
, 0, "lsqlite3_get_eid local context");
827 if (local_ctx
== NULL
) {
831 cdn
= ldb_dn_linearize(local_ctx
, ldb_dn_casefold(module
->ldb
, dn
));
834 eid
= lsqlite3_get_eid_ndn(lsqlite3
->sqlite
, local_ctx
, cdn
);
837 talloc_free(local_ctx
);
842 * Interface functions referenced by lsqlite3_ops
845 static int lsql_search_sync_callback(struct ldb_context
*ldb
, void *context
, struct ldb_async_result
*ares
)
847 struct ldb_result
*res
= NULL
;
850 ldb_set_errstring(ldb
, talloc_strdup(ldb
, "NULL Context in callback"));
854 res
= *((struct ldb_result
**)context
);
860 if (ares
->type
== LDB_REPLY_ENTRY
) {
861 res
->msgs
= talloc_realloc(res
, res
->msgs
, struct ldb_message
*, res
->count
+ 2);
866 res
->msgs
[res
->count
+ 1] = NULL
;
868 res
->msgs
[res
->count
] = talloc_steal(res
->msgs
, ares
->message
);
869 if (! res
->msgs
[res
->count
]) {
875 ldb_debug(ldb
, LDB_DEBUG_ERROR
, "unrecognized async reply in ltdb_search_sync_callback!\n");
883 if (ares
) talloc_free(ares
);
884 if (res
) talloc_free(res
);
885 if (context
) *((struct ldb_result
**)context
) = NULL
;
886 return LDB_ERR_OPERATIONS_ERROR
;
889 /* search for matching records, by tree */
890 int lsql_search_async(struct ldb_module
*module
, const struct ldb_dn
*base
,
891 enum ldb_scope scope
, struct ldb_parse_tree
*tree
,
892 const char * const *attrs
,
894 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*),
895 struct ldb_async_handle
**handle
)
897 struct lsqlite3_private
*lsqlite3
= talloc_get_type(module
->private_data
, struct lsqlite3_private
);
898 struct lsql_async_context
*lsql_ac
;
905 *handle
= init_handle(lsqlite3
, module
, context
, callback
);
906 if (*handle
== NULL
) {
907 talloc_free(*handle
);
908 return LDB_ERR_OPERATIONS_ERROR
;
911 lsql_ac
= talloc_get_type((*handle
)->private_data
, struct lsql_async_context
);
914 norm_basedn
= ldb_dn_linearize(lsql_ac
, ldb_dn_casefold(module
->ldb
, base
));
915 if (norm_basedn
== NULL
) {
916 ret
= LDB_ERR_INVALID_DN_SYNTAX
;
919 } else norm_basedn
= talloc_strdup(lsql_ac
, "");
921 if (*norm_basedn
== '\0' &&
922 (scope
== LDB_SCOPE_BASE
|| scope
== LDB_SCOPE_ONELEVEL
)) {
923 ret
= LDB_ERR_UNWILLING_TO_PERFORM
;
927 /* Convert filter into a series of SQL conditions (constraints) */
928 sqlfilter
= parsetree_to_sql(module
, lsql_ac
, tree
);
931 case LDB_SCOPE_DEFAULT
:
932 case LDB_SCOPE_SUBTREE
:
933 if (*norm_basedn
!= '\0') {
934 query
= lsqlite3_tprintf(lsql_ac
,
935 "SELECT entry.eid,\n"
939 " FROM ldb_entry AS entry\n"
941 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
942 " ON av.eid = entry.eid\n"
944 " WHERE entry.eid IN\n"
945 " (SELECT DISTINCT ldb_entry.eid\n"
947 " WHERE (ldb_entry.norm_dn GLOB('*,%q')\n"
948 " OR ldb_entry.norm_dn = '%q')\n"
949 " AND ldb_entry.eid IN\n"
953 " ORDER BY entry.eid ASC;",
958 query
= lsqlite3_tprintf(lsql_ac
,
959 "SELECT entry.eid,\n"
963 " FROM ldb_entry AS entry\n"
965 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
966 " ON av.eid = entry.eid\n"
968 " WHERE entry.eid IN\n"
969 " (SELECT DISTINCT ldb_entry.eid\n"
971 " WHERE ldb_entry.eid IN\n"
975 " ORDER BY entry.eid ASC;",
982 query
= lsqlite3_tprintf(lsql_ac
,
983 "SELECT entry.eid,\n"
987 " FROM ldb_entry AS entry\n"
989 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
990 " ON av.eid = entry.eid\n"
992 " WHERE entry.eid IN\n"
993 " (SELECT DISTINCT ldb_entry.eid\n"
995 " WHERE ldb_entry.norm_dn = '%q'\n"
996 " AND ldb_entry.eid IN\n"
1000 " ORDER BY entry.eid ASC;",
1005 case LDB_SCOPE_ONELEVEL
:
1006 query
= lsqlite3_tprintf(lsql_ac
,
1007 "SELECT entry.eid,\n"
1011 " FROM ldb_entry AS entry\n"
1013 " LEFT OUTER JOIN ldb_attribute_values AS av\n"
1014 " ON av.eid = entry.eid\n"
1016 " WHERE entry.eid IN\n"
1017 " (SELECT DISTINCT ldb_entry.eid\n"
1019 " WHERE norm_dn GLOB('*,%q')\n"
1020 " AND NOT norm_dn GLOB('*,*,%q')\n"
1021 " AND ldb_entry.eid IN\n(%s)\n"
1024 " ORDER BY entry.eid ASC;",
1031 if (query
== NULL
) {
1036 printf ("%s\n", query);
1039 lsql_ac
->current_eid
= 0;
1040 lsql_ac
->attrs
= attrs
;
1041 lsql_ac
->ares
= NULL
;
1043 (*handle
)->state
= LDB_ASYNC_PENDING
;
1045 ret
= sqlite3_exec(lsqlite3
->sqlite
, query
, lsqlite3_search_callback
, *handle
, &errmsg
);
1046 if (ret
!= SQLITE_OK
) {
1048 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1054 /* complete the last message if any */
1055 if (lsql_ac
->ares
) {
1056 lsql_ac
->ares
->message
= ldb_msg_canonicalize(module
->ldb
, lsql_ac
->ares
->message
);
1057 if (lsql_ac
->ares
->message
== NULL
)
1060 (*handle
)->status
= lsql_ac
->callback(module
->ldb
, lsql_ac
->context
, lsql_ac
->ares
);
1061 if ((*handle
)->status
!= LDB_SUCCESS
)
1065 (*handle
)->state
= LDB_ASYNC_DONE
;
1070 talloc_free(*handle
);
1071 return LDB_ERR_OPERATIONS_ERROR
;
1074 static int lsql_search_bytree(struct ldb_module
* module
, const struct ldb_dn
* base
,
1075 enum ldb_scope scope
, struct ldb_parse_tree
* tree
,
1076 const char * const * attrs
, struct ldb_result
** res
)
1078 struct ldb_async_handle
*handle
;
1081 *res
= talloc_zero(module
, struct ldb_result
);
1083 return LDB_ERR_OPERATIONS_ERROR
;
1086 ret
= lsql_search_async(module
, base
, scope
, tree
, attrs
,
1087 res
, &lsql_search_sync_callback
,
1090 if (ret
== LDB_SUCCESS
) {
1091 ret
= ldb_async_wait(handle
, LDB_WAIT_ALL
);
1092 talloc_free(handle
);
1095 if (ret
!= LDB_SUCCESS
) {
1103 static int lsql_add_async(struct ldb_module
*module
, struct ldb_message
*msg
,
1105 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*),
1106 struct ldb_async_handle
**handle
)
1108 struct lsqlite3_private
*lsqlite3
= talloc_get_type(module
->private_data
, struct lsqlite3_private
);
1109 struct lsql_async_context
*lsql_ac
;
1115 int ret
= LDB_ERR_OPERATIONS_ERROR
;
1117 *handle
= init_handle(lsqlite3
, module
, context
, callback
);
1118 if (*handle
== NULL
) {
1121 lsql_ac
= talloc_get_type((*handle
)->private_data
, struct lsql_async_context
);
1122 (*handle
)->state
= LDB_ASYNC_DONE
;
1123 (*handle
)->status
= LDB_SUCCESS
;
1125 /* See if this is an ltdb special */
1126 if (ldb_dn_is_special(msg
->dn
)) {
1129 c
= ldb_dn_explode(lsql_ac
, "@SUBCLASSES");
1130 if (ldb_dn_compare(module
->ldb
, msg
->dn
, c
) == 0) {
1131 #warning "insert subclasses into object class tree"
1132 ret
= LDB_ERR_UNWILLING_TO_PERFORM
;
1137 c = ldb_dn_explode(local_ctx, "@INDEXLIST");
1138 if (ldb_dn_compare(module->ldb, msg->dn, c) == 0) {
1139 #warning "should we handle indexes somehow ?"
1143 /* Others are implicitly ignored */
1147 /* create linearized and normalized dns */
1148 dn
= ldb_dn_linearize(lsql_ac
, msg
->dn
);
1149 ndn
= ldb_dn_linearize(lsql_ac
, ldb_dn_casefold(module
->ldb
, msg
->dn
));
1150 if (dn
== NULL
|| ndn
== NULL
) {
1151 ret
= LDB_ERR_OTHER
;
1155 query
= lsqlite3_tprintf(lsql_ac
,
1157 "INSERT OR ABORT INTO ldb_entry "
1158 "('dn', 'norm_dn') "
1159 "VALUES ('%q', '%q');",
1161 if (query
== NULL
) {
1162 ret
= LDB_ERR_OTHER
;
1166 ret
= sqlite3_exec(lsqlite3
->sqlite
, query
, NULL
, NULL
, &errmsg
);
1167 if (ret
!= SQLITE_OK
) {
1169 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1172 ret
= LDB_ERR_OTHER
;
1176 eid
= lsqlite3_get_eid_ndn(lsqlite3
->sqlite
, lsql_ac
, ndn
);
1178 ret
= LDB_ERR_OTHER
;
1182 for (i
= 0; i
< msg
->num_elements
; i
++) {
1183 const struct ldb_message_element
*el
= &msg
->elements
[i
];
1184 const struct ldb_attrib_handler
*h
;
1188 /* Get a case-folded copy of the attribute name */
1189 attr
= ldb_attr_casefold(lsql_ac
, el
->name
);
1191 ret
= LDB_ERR_OTHER
;
1195 h
= ldb_attrib_handler(module
->ldb
, el
->name
);
1197 /* For each value of the specified attribute name... */
1198 for (j
= 0; j
< el
->num_values
; j
++) {
1199 struct ldb_val value
;
1202 /* Get a canonicalised copy of the data */
1203 h
->canonicalise_fn(module
->ldb
, lsql_ac
, &(el
->values
[j
]), &value
);
1204 if (value
.data
== NULL
) {
1205 ret
= LDB_ERR_OTHER
;
1209 insert
= lsqlite3_tprintf(lsql_ac
,
1210 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1211 "('eid', 'attr_name', 'norm_attr_name',"
1212 " 'attr_value', 'norm_attr_value') "
1213 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1214 eid
, el
->name
, attr
,
1215 el
->values
[j
].data
, value
.data
);
1216 if (insert
== NULL
) {
1217 ret
= LDB_ERR_OTHER
;
1221 ret
= sqlite3_exec(lsqlite3
->sqlite
, insert
, NULL
, NULL
, &errmsg
);
1222 if (ret
!= SQLITE_OK
) {
1224 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1227 ret
= LDB_ERR_OTHER
;
1233 if (lsql_ac
->callback
)
1234 (*handle
)->status
= lsql_ac
->callback(module
->ldb
, lsql_ac
->context
, NULL
);
1239 talloc_free(*handle
);
1243 static int lsql_add(struct ldb_module
*module
, const struct ldb_message
*msg
)
1245 struct ldb_async_handle
*handle
;
1248 ret
= lsql_add_async(module
, msg
, NULL
, NULL
, &handle
);
1250 if (ret
!= LDB_SUCCESS
)
1253 ret
= ldb_async_wait(handle
, LDB_WAIT_ALL
);
1255 talloc_free(handle
);
1260 /* modify a record */
1261 static int lsql_modify_async(struct ldb_module
*module
, const struct ldb_message
*msg
,
1263 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*),
1264 struct ldb_async_handle
**handle
)
1266 struct lsqlite3_private
*lsqlite3
= talloc_get_type(module
->private_data
, struct lsqlite3_private
);
1267 struct lsql_async_context
*lsql_ac
;
1271 int ret
= LDB_ERR_OPERATIONS_ERROR
;
1273 *handle
= init_handle(lsqlite3
, module
, context
, callback
);
1274 if (*handle
== NULL
) {
1277 lsql_ac
= talloc_get_type((*handle
)->private_data
, struct lsql_async_context
);
1278 (*handle
)->state
= LDB_ASYNC_DONE
;
1279 (*handle
)->status
= LDB_SUCCESS
;
1281 /* See if this is an ltdb special */
1282 if (ldb_dn_is_special(msg
->dn
)) {
1285 c
= ldb_dn_explode(lsql_ac
, "@SUBCLASSES");
1286 if (ldb_dn_compare(module
->ldb
, msg
->dn
, c
) == 0) {
1287 #warning "modify subclasses into object class tree"
1288 ret
= LDB_ERR_UNWILLING_TO_PERFORM
;
1292 /* Others are implicitly ignored */
1296 eid
= lsqlite3_get_eid(module
, msg
->dn
);
1298 ret
= LDB_ERR_OTHER
;
1302 for (i
= 0; i
< msg
->num_elements
; i
++) {
1303 const struct ldb_message_element
*el
= &msg
->elements
[i
];
1304 const struct ldb_attrib_handler
*h
;
1305 int flags
= el
->flags
& LDB_FLAG_MOD_MASK
;
1310 /* Get a case-folded copy of the attribute name */
1311 attr
= ldb_attr_casefold(lsql_ac
, el
->name
);
1313 ret
= LDB_ERR_OTHER
;
1317 h
= ldb_attrib_handler(module
->ldb
, el
->name
);
1321 case LDB_FLAG_MOD_REPLACE
:
1323 /* remove all attributes before adding the replacements */
1324 mod
= lsqlite3_tprintf(lsql_ac
,
1325 "DELETE FROM ldb_attribute_values "
1326 "WHERE eid = '%lld' "
1327 "AND norm_attr_name = '%q';",
1330 ret
= LDB_ERR_OTHER
;
1334 ret
= sqlite3_exec(lsqlite3
->sqlite
, mod
, NULL
, NULL
, &errmsg
);
1335 if (ret
!= SQLITE_OK
) {
1337 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1340 ret
= LDB_ERR_OTHER
;
1344 /* MISSING break is INTENTIONAL */
1346 case LDB_FLAG_MOD_ADD
:
1347 #warning "We should throw an error if no value is provided!"
1348 /* For each value of the specified attribute name... */
1349 for (j
= 0; j
< el
->num_values
; j
++) {
1350 struct ldb_val value
;
1352 /* Get a canonicalised copy of the data */
1353 h
->canonicalise_fn(module
->ldb
, lsql_ac
, &(el
->values
[j
]), &value
);
1354 if (value
.data
== NULL
) {
1355 ret
= LDB_ERR_OTHER
;
1359 mod
= lsqlite3_tprintf(lsql_ac
,
1360 "INSERT OR ROLLBACK INTO ldb_attribute_values "
1361 "('eid', 'attr_name', 'norm_attr_name',"
1362 " 'attr_value', 'norm_attr_value') "
1363 "VALUES ('%lld', '%q', '%q', '%q', '%q');",
1364 eid
, el
->name
, attr
,
1365 el
->values
[j
].data
, value
.data
);
1368 ret
= LDB_ERR_OTHER
;
1372 ret
= sqlite3_exec(lsqlite3
->sqlite
, mod
, NULL
, NULL
, &errmsg
);
1373 if (ret
!= SQLITE_OK
) {
1375 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1378 ret
= LDB_ERR_OTHER
;
1385 case LDB_FLAG_MOD_DELETE
:
1386 #warning "We should throw an error if the attribute we are trying to delete does not exist!"
1387 if (el
->num_values
== 0) {
1388 mod
= lsqlite3_tprintf(lsql_ac
,
1389 "DELETE FROM ldb_attribute_values "
1390 "WHERE eid = '%lld' "
1391 "AND norm_attr_name = '%q';",
1394 ret
= LDB_ERR_OTHER
;
1398 ret
= sqlite3_exec(lsqlite3
->sqlite
, mod
, NULL
, NULL
, &errmsg
);
1399 if (ret
!= SQLITE_OK
) {
1401 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1404 ret
= LDB_ERR_OTHER
;
1409 /* For each value of the specified attribute name... */
1410 for (j
= 0; j
< el
->num_values
; j
++) {
1411 struct ldb_val value
;
1413 /* Get a canonicalised copy of the data */
1414 h
->canonicalise_fn(module
->ldb
, lsql_ac
, &(el
->values
[j
]), &value
);
1415 if (value
.data
== NULL
) {
1416 ret
= LDB_ERR_OTHER
;
1420 mod
= lsqlite3_tprintf(lsql_ac
,
1421 "DELETE FROM ldb_attribute_values "
1422 "WHERE eid = '%lld' "
1423 "AND norm_attr_name = '%q' "
1424 "AND norm_attr_value = '%q';",
1425 eid
, attr
, value
.data
);
1428 ret
= LDB_ERR_OTHER
;
1432 ret
= sqlite3_exec(lsqlite3
->sqlite
, mod
, NULL
, NULL
, &errmsg
);
1433 if (ret
!= SQLITE_OK
) {
1435 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1438 ret
= LDB_ERR_OTHER
;
1447 if (lsql_ac
->callback
)
1448 (*handle
)->status
= lsql_ac
->callback(module
->ldb
, lsql_ac
->context
, NULL
);
1453 talloc_free(*handle
);
1457 static int lsql_modify(struct ldb_module
*module
, const struct ldb_message
*msg
)
1459 struct ldb_async_handle
*handle
;
1462 ret
= lsql_modify_async(module
, msg
, NULL
, NULL
, &handle
);
1464 if (ret
!= LDB_SUCCESS
)
1467 ret
= ldb_async_wait(handle
, LDB_WAIT_ALL
);
1469 talloc_free(handle
);
1473 /* delete a record */
1474 static int lsql_delete_async(struct ldb_module
*module
, const struct ldb_dn
*dn
,
1476 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*),
1477 struct ldb_async_handle
**handle
)
1479 struct lsqlite3_private
*lsqlite3
= talloc_get_type(module
->private_data
, struct lsqlite3_private
);
1480 struct lsql_async_context
*lsql_ac
;
1484 int ret
= LDB_ERR_OPERATIONS_ERROR
;
1487 *handle
= init_handle(lsqlite3
, module
, context
, callback
);
1488 if (*handle
== NULL
) {
1491 lsql_ac
= talloc_get_type((*handle
)->private_data
, struct lsql_async_context
);
1492 (*handle
)->state
= LDB_ASYNC_DONE
;
1493 (*handle
)->status
= LDB_SUCCESS
;
1495 eid
= lsqlite3_get_eid(module
, dn
);
1500 query
= lsqlite3_tprintf(lsql_ac
,
1502 "DELETE FROM ldb_entry WHERE eid = %lld; "
1503 /* Delete attributes */
1504 "DELETE FROM ldb_attribute_values WHERE eid = %lld; ",
1506 if (query
== NULL
) {
1507 ret
= LDB_ERR_OTHER
;
1511 ret
= sqlite3_exec(lsqlite3
->sqlite
, query
, NULL
, NULL
, &errmsg
);
1512 if (ret
!= SQLITE_OK
) {
1514 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1517 ret
= LDB_ERR_OPERATIONS_ERROR
;
1521 if (lsql_ac
->callback
)
1522 (*handle
)->status
= lsql_ac
->callback(module
->ldb
, lsql_ac
->context
, NULL
);
1527 talloc_free(*handle
);
1531 static int lsql_delete(struct ldb_module
*module
, const struct ldb_dn
*dn
)
1533 struct ldb_async_handle
*handle
;
1536 /* ignore ltdb specials */
1537 if (ldb_dn_is_special(dn
)) {
1541 ret
= lsql_delete_async(module
, dn
, NULL
, NULL
, &handle
);
1543 if (ret
!= LDB_SUCCESS
)
1546 ret
= ldb_async_wait(handle
, LDB_WAIT_ALL
);
1548 talloc_free(handle
);
1552 /* rename a record */
1553 static int lsql_rename_async(struct ldb_module
*module
, const struct ldb_dn
*olddn
, const struct ldb_dn
*newdn
,
1555 int (*callback
)(struct ldb_context
*, void *, struct ldb_async_result
*),
1556 struct ldb_async_handle
**handle
)
1558 struct lsqlite3_private
*lsqlite3
= talloc_get_type(module
->private_data
, struct lsqlite3_private
);
1559 struct lsql_async_context
*lsql_ac
;
1560 char *new_dn
, *new_cdn
, *old_cdn
;
1563 int ret
= LDB_ERR_OPERATIONS_ERROR
;
1565 *handle
= init_handle(lsqlite3
, module
, context
, callback
);
1566 if (*handle
== NULL
) {
1569 lsql_ac
= talloc_get_type((*handle
)->private_data
, struct lsql_async_context
);
1570 (*handle
)->state
= LDB_ASYNC_DONE
;
1571 (*handle
)->status
= LDB_SUCCESS
;
1573 /* create linearized and normalized dns */
1574 old_cdn
= ldb_dn_linearize(lsql_ac
, ldb_dn_casefold(module
->ldb
, olddn
));
1575 new_cdn
= ldb_dn_linearize(lsql_ac
, ldb_dn_casefold(module
->ldb
, newdn
));
1576 new_dn
= ldb_dn_linearize(lsql_ac
, newdn
);
1577 if (old_cdn
== NULL
|| new_cdn
== NULL
|| new_dn
== NULL
) {
1581 /* build the SQL query */
1582 query
= lsqlite3_tprintf(lsql_ac
,
1583 "UPDATE ldb_entry SET dn = '%q', norm_dn = '%q' "
1584 "WHERE norm_dn = '%q';",
1585 new_dn
, new_cdn
, old_cdn
);
1586 if (query
== NULL
) {
1591 ret
= sqlite3_exec(lsqlite3
->sqlite
, query
, NULL
, NULL
, &errmsg
);
1592 if (ret
!= SQLITE_OK
) {
1594 ldb_set_errstring(module
->ldb
, talloc_strdup(module
, errmsg
));
1597 ret
= LDB_ERR_OPERATIONS_ERROR
;
1601 if (lsql_ac
->callback
)
1602 (*handle
)->status
= lsql_ac
->callback(module
->ldb
, lsql_ac
->context
, NULL
);
1607 talloc_free(*handle
);
1611 static int lsql_rename(struct ldb_module
*module
, const struct ldb_dn
*olddn
, const struct ldb_dn
*newdn
)
1613 struct ldb_async_handle
*handle
;
1616 /* ignore ltdb specials */
1617 if (ldb_dn_is_special(olddn
) || ldb_dn_is_special(newdn
)) {
1622 ret
= lsql_rename_async(module
, olddn
, newdn
, NULL
, NULL
, &handle
);
1624 if (ret
!= LDB_SUCCESS
)
1627 ret
= ldb_async_wait(handle
, LDB_WAIT_ALL
);
1629 talloc_free(handle
);
1633 static int lsql_start_trans(struct ldb_module
* module
)
1637 struct lsqlite3_private
* lsqlite3
= module
->private_data
;
1639 if (lsqlite3
->trans_count
== 0) {
1640 ret
= sqlite3_exec(lsqlite3
->sqlite
, "BEGIN IMMEDIATE;", NULL
, NULL
, &errmsg
);
1641 if (ret
!= SQLITE_OK
) {
1643 printf("lsqlite3_start_trans: error: %s\n", errmsg
);
1650 lsqlite3
->trans_count
++;
1655 static int lsql_end_trans(struct ldb_module
*module
)
1659 struct lsqlite3_private
*lsqlite3
= module
->private_data
;
1661 if (lsqlite3
->trans_count
> 0) {
1662 lsqlite3
->trans_count
--;
1665 if (lsqlite3
->trans_count
== 0) {
1666 ret
= sqlite3_exec(lsqlite3
->sqlite
, "COMMIT;", NULL
, NULL
, &errmsg
);
1667 if (ret
!= SQLITE_OK
) {
1669 printf("lsqlite3_end_trans: error: %s\n", errmsg
);
1679 static int lsql_del_trans(struct ldb_module
*module
)
1681 struct lsqlite3_private
*lsqlite3
= module
->private_data
;
1683 if (lsqlite3
->trans_count
> 0) {
1684 lsqlite3
->trans_count
--;
1687 if (lsqlite3
->trans_count
== 0) {
1688 return lsqlite3_safe_rollback(lsqlite3
->sqlite
);
1698 static int initialize(struct lsqlite3_private
*lsqlite3
,
1699 struct ldb_context
*ldb
, const char *url
, int flags
)
1701 TALLOC_CTX
*local_ctx
;
1708 /* create a local ctx */
1709 local_ctx
= talloc_named(lsqlite3
, 0, "lsqlite3_rename local context");
1710 if (local_ctx
== NULL
) {
1714 schema
= lsqlite3_tprintf(local_ctx
,
1717 "CREATE TABLE ldb_info AS "
1718 " SELECT 'LDB' AS database_type,"
1719 " '1.0' AS version;"
1722 * The entry table holds the information about an entry.
1723 * This table is used to obtain the EID of the entry and to
1724 * support scope=one and scope=base. The parent and child
1725 * table is included in the entry table since all the other
1726 * attributes are dependent on EID.
1728 "CREATE TABLE ldb_entry "
1730 " eid INTEGER PRIMARY KEY AUTOINCREMENT,"
1731 " dn TEXT UNIQUE NOT NULL,"
1732 " norm_dn TEXT UNIQUE NOT NULL"
1736 "CREATE TABLE ldb_object_classes"
1738 " class_name TEXT PRIMARY KEY,"
1739 " parent_class_name TEXT,"
1740 " tree_key TEXT UNIQUE,"
1741 " max_child_num INTEGER DEFAULT 0"
1745 * We keep a full listing of attribute/value pairs here
1747 "CREATE TABLE ldb_attribute_values"
1749 " eid INTEGER REFERENCES ldb_entry,"
1751 " norm_attr_name TEXT,"
1753 " norm_attr_value TEXT "
1760 "CREATE INDEX ldb_attribute_values_eid_idx "
1761 " ON ldb_attribute_values (eid);"
1763 "CREATE INDEX ldb_attribute_values_name_value_idx "
1764 " ON ldb_attribute_values (attr_name, norm_attr_value);"
1772 "CREATE TRIGGER ldb_object_classes_insert_tr"
1774 " ON ldb_object_classes"
1777 " UPDATE ldb_object_classes"
1778 " SET tree_key = COALESCE(tree_key, "
1780 " SELECT tree_key || "
1781 " (SELECT base160(max_child_num + 1)"
1782 " FROM ldb_object_classes"
1783 " WHERE class_name = "
1784 " new.parent_class_name)"
1785 " FROM ldb_object_classes "
1786 " WHERE class_name = new.parent_class_name "
1788 " UPDATE ldb_object_classes "
1789 " SET max_child_num = max_child_num + 1"
1790 " WHERE class_name = new.parent_class_name;"
1794 * Table initialization
1797 "INSERT INTO ldb_object_classes "
1798 " (class_name, tree_key) "
1800 " ('TOP', '0001');");
1802 /* Skip protocol indicator of url */
1803 if (strncmp(url
, "sqlite://", 9) != 0) {
1804 return SQLITE_MISUSE
;
1807 /* Update pointer to just after the protocol indicator */
1810 /* Try to open the (possibly empty/non-existent) database */
1811 if ((ret
= sqlite3_open(url
, &lsqlite3
->sqlite
)) != SQLITE_OK
) {
1815 /* In case this is a new database, enable auto_vacuum */
1816 ret
= sqlite3_exec(lsqlite3
->sqlite
, "PRAGMA auto_vacuum = 1;", NULL
, NULL
, &errmsg
);
1817 if (ret
!= SQLITE_OK
) {
1819 printf("lsqlite3 initializaion error: %s\n", errmsg
);
1825 if (flags
& LDB_FLG_NOSYNC
) {
1827 ret
= sqlite3_exec(lsqlite3
->sqlite
, "PRAGMA synchronous = OFF;", NULL
, NULL
, &errmsg
);
1828 if (ret
!= SQLITE_OK
) {
1830 printf("lsqlite3 initializaion error: %s\n", errmsg
);
1839 /* Establish a busy timeout of 30 seconds */
1840 if ((ret
= sqlite3_busy_timeout(lsqlite3
->sqlite
,
1841 30000)) != SQLITE_OK
) {
1845 /* Create a function, callable from sql, to increment a tree_key */
1847 sqlite3_create_function(lsqlite3
->sqlite
,/* handle */
1848 "base160_next", /* function name */
1849 1, /* number of args */
1850 SQLITE_ANY
, /* preferred text type */
1851 NULL
, /* user data */
1852 base160next_sql
, /* called func */
1853 NULL
, /* step func */
1854 NULL
/* final func */
1859 /* Create a function, callable from sql, to convert int to base160 */
1861 sqlite3_create_function(lsqlite3
->sqlite
,/* handle */
1862 "base160", /* function name */
1863 1, /* number of args */
1864 SQLITE_ANY
, /* preferred text type */
1865 NULL
, /* user data */
1866 base160_sql
, /* called func */
1867 NULL
, /* step func */
1868 NULL
/* final func */
1873 /* Create a function, callable from sql, to perform various comparisons */
1875 sqlite3_create_function(lsqlite3
->sqlite
, /* handle */
1876 "ldap_compare", /* function name */
1877 4, /* number of args */
1878 SQLITE_ANY
, /* preferred text type */
1879 ldb
, /* user data */
1880 lsqlite3_compare
, /* called func */
1881 NULL
, /* step func */
1882 NULL
/* final func */
1887 /* Begin a transaction */
1888 ret
= sqlite3_exec(lsqlite3
->sqlite
, "BEGIN EXCLUSIVE;", NULL
, NULL
, &errmsg
);
1889 if (ret
!= SQLITE_OK
) {
1891 printf("lsqlite3: initialization error: %s\n", errmsg
);
1898 /* Determine if this is a new database. No tables means it is. */
1899 if (query_int(lsqlite3
,
1902 " FROM sqlite_master\n"
1903 " WHERE type = 'table';") != 0) {
1907 if (queryInt
== 0) {
1909 * Create the database schema
1911 ret
= sqlite3_exec(lsqlite3
->sqlite
, schema
, NULL
, NULL
, &errmsg
);
1912 if (ret
!= SQLITE_OK
) {
1914 printf("lsqlite3 initializaion error: %s\n", errmsg
);
1921 * Ensure that the database we opened is one of ours
1923 if (query_int(lsqlite3
,
1926 " (SELECT COUNT(*) = 2"
1927 " FROM sqlite_master "
1928 " WHERE type = 'table' "
1932 " 'ldb_object_classes' "
1938 " WHERE database_type = 'LDB' "
1939 " AND version = '1.0'"
1943 /* It's not one that we created. See ya! */
1948 /* Commit the transaction */
1949 ret
= sqlite3_exec(lsqlite3
->sqlite
, "COMMIT;", NULL
, NULL
, &errmsg
);
1950 if (ret
!= SQLITE_OK
) {
1952 printf("lsqlite3: iniialization error: %s\n", errmsg
);
1961 if (rollback
) lsqlite3_safe_rollback(lsqlite3
->sqlite
);
1962 sqlite3_close(lsqlite3
->sqlite
);
1966 static int destructor(struct lsqlite3_private
*lsqlite3
)
1968 if (lsqlite3
->sqlite
) {
1969 sqlite3_close(lsqlite3
->sqlite
);
1974 static int lsql_async_wait(struct ldb_async_handle
*handle
, enum ldb_async_wait_type type
)
1976 return handle
->status
;
1979 static int lsql_request(struct ldb_module
*module
, struct ldb_request
*req
)
1981 /* check for oustanding critical controls and return an error if found */
1983 if (req
->controls
!= NULL
) {
1984 ldb_debug(module
->ldb
, LDB_DEBUG_WARNING
, "Controls should not reach the ldb_sqlite3 backend!\n");
1987 if (check_critical_controls(req
->controls
)) {
1988 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
;
1991 switch (req
->operation
) {
1994 return lsql_search_bytree(module
,
1995 req
->op
.search
.base
,
1996 req
->op
.search
.scope
,
1997 req
->op
.search
.tree
,
1998 req
->op
.search
.attrs
,
1999 &req
->op
.search
.res
);
2002 return lsql_add(module
, req
->op
.add
.message
);
2005 return lsql_modify(module
, req
->op
.mod
.message
);
2008 return lsql_delete(module
, req
->op
.del
.dn
);
2011 return lsql_rename(module
,
2012 req
->op
.rename
.olddn
,
2013 req
->op
.rename
.newdn
);
2016 return lsql_search_async(module
,
2017 req
->op
.search
.base
,
2018 req
->op
.search
.scope
,
2019 req
->op
.search
.tree
,
2020 req
->op
.search
.attrs
,
2022 req
->async
.callback
,
2023 &req
->async
.handle
);
2026 return lsql_add_async(module,
2027 req->op.add.message,
2029 req->async.callback,
2030 &req->async.handle);
2033 return lsql_modify_async(module,
2034 req->op.mod.message,
2036 req->async.callback,
2037 &req->async.handle);
2040 return lsql_delete_async(module
,
2043 req
->async
.callback
,
2044 &req
->async
.handle
);
2047 return lsql_rename_async(module
,
2048 req
->op
.rename
.olddn
,
2049 req
->op
.rename
.newdn
,
2051 req
->async
.callback
,
2052 &req
->async
.handle
);
2055 return LDB_ERR_OPERATIONS_ERROR
;
2061 * Table of operations for the sqlite3 backend
2063 static const struct ldb_module_ops lsqlite3_ops
= {
2065 .request
= lsql_request
,
2066 .start_transaction
= lsql_start_trans
,
2067 .end_transaction
= lsql_end_trans
,
2068 .del_transaction
= lsql_del_trans
,
2069 .async_wait
= lsql_async_wait
,
2073 * connect to the database
2075 static int lsqlite3_connect(struct ldb_context
*ldb
,
2078 const char *options
[],
2079 struct ldb_module
**module
)
2083 struct lsqlite3_private
* lsqlite3
= NULL
;
2085 lsqlite3
= talloc(ldb
, struct lsqlite3_private
);
2090 lsqlite3
->sqlite
= NULL
;
2091 lsqlite3
->options
= NULL
;
2092 lsqlite3
->trans_count
= 0;
2094 ret
= initialize(lsqlite3
, ldb
, url
, flags
);
2095 if (ret
!= SQLITE_OK
) {
2099 talloc_set_destructor(lsqlite3
, destructor
);
2103 *module
= talloc(ldb
, struct ldb_module
);
2108 (*module
)->ldb
= ldb
;
2109 (*module
)->prev
= (*module
)->next
= NULL
;
2110 (*module
)->private_data
= lsqlite3
;
2111 (*module
)->ops
= &lsqlite3_ops
;
2115 * take a copy of the options array, so we don't have to rely
2116 * on the caller keeping it around (it might be dynamic)
2118 for (i
=0;options
[i
];i
++) ;
2120 lsqlite3
->options
= talloc_array(lsqlite3
, char *, i
+1);
2121 if (!lsqlite3
->options
) {
2125 for (i
=0;options
[i
];i
++) {
2127 lsqlite3
->options
[i
+1] = NULL
;
2128 lsqlite3
->options
[i
] =
2129 talloc_strdup(lsqlite3
->options
, options
[i
]);
2130 if (!lsqlite3
->options
[i
]) {
2139 if (lsqlite3
->sqlite
!= NULL
) {
2140 (void) sqlite3_close(lsqlite3
->sqlite
);
2142 talloc_free(lsqlite3
);
2146 int ldb_sqlite3_init(void)
2148 return ldb_register_backend("sqlite3", lsqlite3_connect
);