r7438: work in progress
[metze/samba/wip.git] / source4 / lib / ldb / ldb_sqlite3 / ldb_sqlite3.c
1 /* 
2    ldb database library
3    
4    Copyright (C) Derrell Lipman  2005
5    
6    ** NOTE! The following LGPL license applies to the ldb
7    ** library. This does NOT imply that all of Samba is released
8    ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 2 of the License, or (at your option) any later version.
14    
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19    
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb
27  *
28  *  Component: ldb sqlite3 backend
29  *
30  *  Description: core files for SQLITE3 backend
31  *
32  *  Author: Derrell Lipman (based on Andrew Tridgell's LDAP backend)
33  */
34
35 #include <stdarg.h>
36 #include "includes.h"
37 #include "ldb/include/ldb.h"
38 #include "ldb/include/ldb_private.h"
39 #include "ldb/include/ldb_parse.h"
40 #include "ldb/include/ldb_explode_dn.h"
41 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
42
43 #ifndef FALSE
44 # define FALSE  (0)
45 # define TRUE   (! FALSE)
46 #endif
47
48 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...)        \
49     do {                                                        \
50             if (lsqlite3_query_norows(lsqlite3, sql) != 0) {    \
51                 if (bRollbackOnError) {                         \
52                         lsqlite3_query_norows(lsqlite3,         \
53                                        "ROLLBACK;");            \
54                 }                                               \
55                 return -1;                                      \
56         }                                                       \
57     } while (0)
58
59 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...)       \
60     do {                                                                \
61             if (lsqlite3_query_int(lsqlite3, &result_var, sql) != 0) {  \
62                     if (bRollbackOnError) {                             \
63                             lsqlite3_query_norows(lsqlite3,             \
64                                                   "ROLLBACK;");         \
65                     }                                                   \
66                     return -1;                                          \
67             }                                                           \
68     } while (0)
69
70 /*
71  * lsqlite3_query_norows()
72  *
73  * This function is used for queries that are not expected to return any rows,
74  * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
75  * There are no provisions here for returning data from rows in a table, so do
76  * not pass SELECT queries to this function.
77  */
78 static int
79 lsqlite3_query_norows(const struct lsqlite3_private *lsqlite3,
80                       const char *pSql,
81                       ...)
82 {
83         int             ret;
84         int             bLoop;
85         char *          p;
86         const char *    pTail;
87         sqlite3_stmt *  pStmt;
88         va_list         args;
89         
90         /* Begin access to variable argument list */
91         va_start(args, pSql);
92
93         /* Format the query */
94         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
95                 return -1;
96         }
97
98         /*
99          * Prepare and execute the SQL statement.  Loop allows retrying on
100          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
101          * requiring retrying the operation.
102          */
103         for (bLoop = TRUE; bLoop; ) {
104
105                 /* Compile the SQL statement into sqlite virtual machine */
106                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
107                                            pTail,
108                                            -1,
109                                            &pStmt,
110                                            &pTail)) != SQLITE_OK) {
111                         ret = -1;
112                         break;
113                 }
114                 
115                 /* No rows expected, so just step through machine code once */
116                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
117                         (void) sqlite3_finalize(pStmt);
118                         continue;
119                 } else if (ret != SQLITE_DONE) {
120                         (void) sqlite3_finalize(pStmt);
121                         ret = -1;
122                         break;
123                 }
124
125                 /* Free the virtual machine */
126                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
127                         (void) sqlite3_finalize(pStmt);
128                         continue;
129                 } else if (ret != SQLITE_OK) {
130                         (void) sqlite3_finalize(pStmt);
131                         ret = -1;
132                         break;
133                 }
134
135                 /*
136                  * Normal condition is only one time through loop.  Loop is
137                  * rerun in error conditions, via "continue", above.
138                  */
139                 ret = 0;
140                 bLoop = FALSE;
141         }
142
143         /* All done with variable argument list */
144         va_end(args);
145
146         /* Free the memory we allocated for our query string */
147         sqlite3_free(p);
148
149         return ret;
150 }
151
152
153 /*
154  * lsqlite3_query_int()
155  *
156  * This function is used for the common case of queries that return a single
157  * integer value.
158  *
159  * NOTE: If more than one value is returned by the query, all but the first
160  * one will be ignored.
161  */
162 static int
163 lsqlite3_query_int(const struct lsqlite3_private * lsqlite3,
164                    long long * pRet,
165                    const char * pSql,
166                    ...)
167 {
168         int             ret;
169         int             bLoop;
170         char *          p;
171         const char *    pTail;
172         sqlite3_stmt *  pStmt;
173         va_list         args;
174         
175         /* Begin access to variable argument list */
176         va_start(args, pSql);
177
178         /* Format the query */
179         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
180                 return -1;
181         }
182
183         /*
184          * Prepare and execute the SQL statement.  Loop allows retrying on
185          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
186          * requiring retrying the operation.
187          */
188         for (bLoop = TRUE; bLoop; ) {
189
190                 /* Compile the SQL statement into sqlite virtual machine */
191                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
192                                            pTail,
193                                            -1,
194                                            &pStmt,
195                                            &pTail)) != SQLITE_OK) {
196                         ret = -1;
197                         break;
198                 }
199                 
200                 /* No rows expected, so just step through machine code once */
201                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
202                         (void) sqlite3_finalize(pStmt);
203                         continue;
204                 } else if (ret != SQLITE_ROW) {
205                         (void) sqlite3_finalize(pStmt);
206                         ret = -1;
207                         break;
208                 }
209
210                 /* Get the value to be returned */
211                 *pRet = sqlite3_column_int64(pStmt, 0);
212
213                 /* Free the virtual machine */
214                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
215                         (void) sqlite3_finalize(pStmt);
216                         continue;
217                 } else if (ret != SQLITE_OK) {
218                         (void) sqlite3_finalize(pStmt);
219                         ret = -1;
220                         break;
221                 }
222
223                 /*
224                  * Normal condition is only one time through loop.  Loop is
225                  * rerun in error conditions, via "continue", above.
226                  */
227                 ret = 0;
228                 bLoop = FALSE;
229         }
230
231         /* All done with variable argument list */
232         va_end(args);
233
234         /* Free the memory we allocated for our query string */
235         sqlite3_free(p);
236
237         return ret;
238 }
239
240
241 #if 0
242 /*
243  * we don't need this right now, but will once we add some backend options
244  *
245  * find an option in an option list (a null terminated list of strings)
246  *
247  * this assumes the list is short. If it ever gets long then we really should
248  * do this in some smarter way
249  */
250 static const char *
251 lsqlite3_option_find(const struct lsqlite3_private *lsqlite3,
252                      const char *name)
253 {
254         int                 i;
255         size_t              len = strlen(name);
256
257         if (!lsqlite3->options) return NULL;
258
259         for (i=0;lsqlite3->options[i];i++) {            
260                 if (strncmp(lsqlite3->options[i], name, len) == 0 &&
261                     lsqlite3->options[i][len] == '=') {
262                         return &lsqlite3->options[i][len+1];
263                 }
264         }
265
266         return NULL;
267 }
268 #endif
269
270 /*
271   callback function used in call to ldb_dn_fold() for determining whether an
272   attribute type requires case folding.
273 */
274 static int lsqlite3_case_fold_attr_required(void * hUserData,
275                                             char *attr)
276 {
277 //        struct ldb_module * module = hUserData;
278         
279 #warning "currently, all attributes require case folding"
280         return TRUE;
281 }
282
283
284 /*
285  * rename a record
286  */
287 static int
288 lsqlite3_rename(struct ldb_module * module,
289                 const char * olddn,
290                 const char * newdn)
291 {
292         /* ignore ltdb specials */
293         if (olddn[0] == '@' ||newdn[0] == '@') {
294                 return 0;
295         }
296
297 #warning "lsqlite3_rename() is not yet supported"
298         return -1;
299 }
300
301 /*
302  * delete a record
303  */
304 static int
305 lsqlite3_delete(struct ldb_module *module,
306                 const char *dn)
307 {
308         /* ignore ltdb specials */
309         if (dn[0] == '@') {
310                 return 0;
311         }
312         
313         return -1;
314 }
315
316 #if 0 /* not currently used */
317 /*
318  * free a search result
319  */
320 static int
321 lsqlite3_search_free(struct ldb_module *module,
322                      struct ldb_message **res)
323 {
324         talloc_free(res);
325         return 0;
326 }
327 #endif
328
329
330 /*
331  * add a single set of ldap message values to a ldb_message
332  */
333
334 /* get things to compile before we actually implement this function */
335 struct berval
336 {
337         int x;
338 };
339
340 #warning "lsqlite3_add_msg_attr() not yet implemented or used"
341 #if 0
342 static int
343 lsqlite3_add_msg_attr(struct ldb_context *ldb,
344                       struct ldb_message *msg, 
345                       const char *attr,
346                       struct berval **bval)
347 {
348         int                          i;
349         int                          count;
350         struct ldb_message_element * el;
351
352         count = ldap_count_values_len(bval);
353
354         if (count <= 0) {
355                 return -1;
356         }
357
358         el = talloc_realloc(msg, msg->elements, struct ldb_message_element, 
359                               msg->num_elements + 1);
360         if (!el) {
361                 errno = ENOMEM;
362                 return -1;
363         }
364
365         msg->elements = el;
366
367         el = &msg->elements[msg->num_elements];
368
369         el->name = talloc_strdup(msg->elements, attr);
370         if (!el->name) {
371                 errno = ENOMEM;
372                 return -1;
373         }
374         el->flags = 0;
375
376         el->num_values = 0;
377         el->values = talloc_array(msg->elements, struct ldb_val, count);
378         if (!el->values) {
379                 errno = ENOMEM;
380                 return -1;
381         }
382
383         for (i=0;i<count;i++) {
384                 el->values[i].data = talloc_memdup(el->values, bval[i]->bv_val, bval[i]->bv_len);
385                 if (!el->values[i].data) {
386                         return -1;
387                 }
388                 el->values[i].length = bval[i]->bv_len;
389                 el->num_values++;
390         }
391
392         msg->num_elements++;
393
394         return 0;
395 }
396 #endif
397
398 static char *
399 lsqlite3_parsetree_to_sql(struct ldb_module *module,
400                           char * hTalloc,
401                           const struct ldb_parse_tree *t)
402 {
403         int                     i;
404         char *                  child;
405         char *                  p;
406         char *                  ret = NULL;
407         char *                  pAttrName;
408         
409
410         switch(t->operation) {
411                 case LDB_OP_SIMPLE:
412                         break;
413
414                 case LDB_OP_AND:
415                         ret = lsqlite3_parsetree_to_sql(module,
416                                                         hTalloc,
417                                                         t->u.list.elements[0]);
418
419                         for (i = 1; i < t->u.list.num_elements; i++) {
420                                 child =
421                                         lsqlite3_parsetree_to_sql(
422                                                 module,
423                                                 hTalloc,
424                                                 t->u.list.elements[i]);
425                                 ret = talloc_asprintf_append(ret,
426                                                              "INTERSECT\n"
427                                                              "%s\n",
428                                                              child);
429                                 talloc_free(child);
430                         }
431
432                         child = ret;
433                         ret = talloc_asprintf("(\n"
434                                               "%s\n"
435                                               ")\n",
436                                               child);
437                         talloc_free(child);
438                         return ret;
439
440                 case LDB_OP_OR:
441                         child =
442                                 lsqlite3_parsetree_to_sql(
443                                         module,
444                                         hTalloc,
445                                         t->u.list.elements[0]);
446
447                         for (i = 1; i < t->u.list.num_elements; i++) {
448                                 child =
449                                         lsqlite3_parsetree_to_sql(
450                                                 module,
451                                                 hTalloc,
452                                                 t->u.list.elements[i]);
453                                 ret = talloc_asprintf_append(ret,
454                                                              "UNION\n"
455                                                              "%s\n",
456                                                              child);
457                                 talloc_free(child);
458                         }
459                         child = ret;
460                         ret = talloc_asprintf("(\n"
461                                               "%s\n"
462                                               ")\n",
463                                               child);
464                         talloc_free(child);
465                         return ret;
466
467                 case LDB_OP_NOT:
468                         child =
469                                 lsqlite3_parsetree_to_sql(
470                                         module,
471                                         hTalloc,
472                                         t->u.not.child);
473                         ret = talloc_asprintf(hTalloc,
474                                               "(\n"
475                                               "  SELECT eid\n"
476                                               "    FROM ldb_entry\n"
477                                               "    WHERE eid NOT IN %s\n"
478                                               ")\n",
479                                               child);
480                         talloc_free(child);
481                         return ret;
482
483                 default:
484                         /* should never occur */
485                         abort();
486         };
487         
488         /* Get a case-folded copy of the attribute name */
489         pAttrName = ldb_casefold((struct ldb_context *) module,
490                                  t->u.simple.attr);
491
492         /*
493          * For simple searches, we want to retrieve the list of EIDs that
494          * match the criteria.  We accomplish this by searching the
495          * appropriate table, ldb_attr_<attributeName>, for the eid
496          * corresponding to all matching values.
497          */
498         if (t->u.simple.value.length == 1 &&
499             (*(const char *) t->u.simple.value.data) == '*') {
500                 /*
501                  * Special case for "attr_name=*".  In this case, we want the
502                  * eid corresponding to all values in the specified attribute
503                  * table.
504                  */
505                 if ((p = sqlite3_mprintf("(\n"
506                                          "  SELECT eid\n"
507                                          "    FROM ldb_attr_%q\n"
508                                          ")\n",
509                                          pAttrName)) == NULL) {
510                         return NULL;
511                 }
512
513                 ret = talloc_strdup(hTalloc, p);
514                 sqlite3_free(p);
515
516         } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
517                 /*
518                  * For object classes, we want to search for all objectclasses
519                  * that are subclasses as well.
520                  */
521                 if ((p = sqlite3_mprintf(
522                              "(\n"
523                              "  SELECT eid\n"
524                              "    FROM ldb_attr_objectclass\n"
525                              "    WHERE attr_name IN\n"
526                              "      (SELECT class_name\n"
527                              "         FROM ldb_objectclasses\n"
528                              "         WHERE tree_key GLOB\n"
529                              "           (SELECT tree_key\n"
530                              "              FROM ldb_objectclasses\n"
531                              "              WHERE class_name = %Q) || '*')\n"
532                              ")\n",
533                              t->u.simple.value.data)) == NULL) {
534                         return NULL;
535                 }
536
537                 ret = talloc_strdup(hTalloc, p);
538                 sqlite3_free(p);
539
540         } else {
541                 /* A normal query. */
542                 if ((p = sqlite3_mprintf("(\n"
543                                          "  SELECT eid\n"
544                                          "    FROM ldb_attr_%q\n"
545                                          "    WHERE attr_value = %Q\n"
546                                          ")\n",
547                                          pAttrName,
548                                          t->u.simple.value.data)) == NULL) {
549                         return NULL;
550                 }
551
552                 ret = talloc_strdup(hTalloc, p);
553                 sqlite3_free(p);
554         }
555         return ret;
556 }
557
558
559 static char *
560 lsqlite3_parsetree_to_tablelist(struct ldb_module *module,
561                                 char * hTalloc,
562                                 const struct ldb_parse_tree *t)
563 {
564 #warning "obtain talloc'ed array of attribute names for table list"
565         return NULL;
566 }
567
568
569 /*
570  * search for matching records
571  */
572 static int
573 lsqlite3_search(struct ldb_module * module,
574                 const char * pBaseDN,
575                 enum ldb_scope scope,
576                 const char * pExpression,
577                 const char * const attrs[],
578                 struct ldb_message *** res)
579 {
580         long long                   eid = 0;
581         char *                      sql;
582         char *                      sql_constraints;
583         char *                      table_list;
584         char *                      hTalloc;
585         struct ldb_parse_tree *     pTree;
586         struct lsqlite3_private *   lsqlite3 = module->private_data;
587         
588         if (pBaseDN == NULL) {
589                 pBaseDN = "";
590         }
591
592         /* Begin a transaction */
593         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN IMMEDIATE;");
594
595         /*
596          * Obtain the eid of the base DN
597          */
598         QUERY_INT(lsqlite3,
599                   eid,
600                   TRUE,
601                   "SELECT eid "
602                   "  FROM ldb_attr_dn "
603                   "  WHERE attr_value = %Q;",
604                   pBaseDN);
605
606         /* Parse the filter expression into a tree we can work with */
607         if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
608                 return -1;
609         }
610         
611         /* Allocate a temporary talloc context */
612         hTalloc = talloc_new(module->ldb);
613
614         /* Move the parse tree to our temporary context */
615         talloc_steal(hTalloc, pTree);
616         
617         /* Convert filter into a series of SQL statements (constraints) */
618         sql_constraints = lsqlite3_parsetree_to_sql(module, hTalloc, pTree);
619         
620         /* Get the list of attribute names to use as our extra table list */
621         table_list = lsqlite3_parsetree_to_tablelist(module, hTalloc, pTree);
622
623         switch(scope) {
624         case LDB_SCOPE_DEFAULT:
625         case LDB_SCOPE_SUBTREE:
626                 sql = sqlite3_mprintf(
627                         "SELECT entry.entry_data\n"
628                         "  FROM ldb_entry AS entry\n"
629                         "  WHERE entry.eid IN\n"
630                         "    (SELECT DISTINCT ldb_entry.eid\n"
631                         "       FROM ldb_entry,\n"
632                         "            ldb_descendants,\n"
633                         "            %q\n"
634                         "       WHERE ldb_descendants.aeid = %lld\n"
635                         "         AND ldb_entry.eid = ldb_descendants.deid\n"
636                         "         AND ldap_entry.eid IN\n"
637                         "%s"
638                         ");",
639                         table_list,
640                         eid,
641                         sql_constraints);
642                 break;
643
644         case LDB_SCOPE_BASE:
645                 sql = sqlite3_mprintf(
646                         "SELECT entry.entry_data\n"
647                         "  FROM ldb_entry AS entry\n"
648                         "  WHERE entry.eid IN\n"
649                         "    (SELECT DISTINCT ldb_entry.eid\n"
650                         "       FROM %q\n"
651                         "       WHERE ldb_entry.eid = %lld\n"
652                         "         AND ldb_entry.eid IN\n"
653                         "%s"
654                         ");",
655                         table_list,
656                         eid,
657                         sql_constraints);
658                 break;
659
660         case LDB_SCOPE_ONELEVEL:
661                 sql = sqlite3_mprintf(
662                         "SELECT entry.entry_data\n"
663                         "  FROM ldb_entry AS entry\n"
664                         "  WHERE entry.eid IN\n"
665                         "    (SELECT DISTINCT ldb_entry.eid\n"
666                         "       FROM ldb_entry AS pchild, "
667                         "            %q\n"
668                         "       WHERE ldb_entry.eid = pchild.eid "
669                         "         AND pchild.peid = %lld "
670                         "         AND ldb_entry.eid IN\n"
671                         "%s"
672                         ");",
673                         table_list,
674                         eid,
675                         sql_constraints);
676                 break;
677         }
678
679 #warning "retrieve and return the result set of the search here"
680
681         /* End the transaction */
682         QUERY_NOROWS(lsqlite3, FALSE, "END TRANSACTION;");
683
684         return 0;
685 }
686
687
688 static int
689 lsqlite3_new_attr(struct ldb_module * module,
690                   char * pAttrName)
691 {
692         struct lsqlite3_private *   lsqlite3 = module->private_data;
693
694         /* NOTE: pAttrName is assumed to already be case-folded here! */
695         QUERY_NOROWS(lsqlite3,
696                      FALSE,
697                      "CREATE TABLE ldb_attr_%q "
698                      "("
699                      "  eid        INTEGER REFERENCES ldb_entry, "
700                      "  attr_value TEXT"
701                      ");",
702                      pAttrName);
703
704         return 0;
705 }
706
707 /*
708  * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
709  * requests in the ldb_message
710  */
711 static int
712 lsqlite3_msg_to_sql(struct ldb_module * module,
713                     const struct ldb_message * msg,
714                     long long eid,
715                     int use_flags)
716 {
717         int                         flags;
718         char *                      pAttrName;
719         unsigned int                i;
720         unsigned int                j;
721         struct lsqlite3_private *   lsqlite3 = module->private_data;
722
723         for (i = 0; i < msg->num_elements; i++) {
724                 const struct ldb_message_element *el = &msg->elements[i];
725
726                 if (! use_flags) {
727                         flags = LDB_FLAG_MOD_ADD;
728                 } else {
729                         flags = el->flags & LDB_FLAG_MOD_MASK;
730                 }
731
732                 /* Get a case-folded copy of the attribute name */
733                 pAttrName = ldb_casefold((struct ldb_context *) module,
734                                          el->name);
735
736                 if (flags == LDB_FLAG_MOD_ADD) {
737                         /* Create the attribute table if it doesn't exist */
738                         if (lsqlite3_new_attr(module, pAttrName) != 0) {
739                                 return -1;
740                         }
741                 }
742
743                 /* For each value of the specified attribute name... */
744                 for (j = 0; j < el->num_values; j++) {
745
746                         /* ... bind the attribute value, if necessary */
747                         switch (flags) {
748                         case LDB_FLAG_MOD_ADD:
749                                 QUERY_NOROWS(lsqlite3,
750                                              FALSE,
751                                              "INSERT INTO ldb_attr_%q "
752                                              "    (eid, attr_value) "
753                                              "  VALUES "
754                                              "    (%lld, %Q);",
755                                              pAttrName,
756                                              eid, el->values[j].data);
757                                 QUERY_NOROWS(lsqlite3,
758                                              FALSE,
759                                              "UPDATE ldb_entry "
760                                              "  SET entry_data = "
761                                              "        add_attr(entry_data, "
762                                              "                 %Q, %Q) "
763                                              "  WHERE eid = %lld;",
764                                              el->name, el->values[j].data,
765                                              eid);
766                                       
767                                 break;
768
769                         case LDB_FLAG_MOD_REPLACE:
770                                 QUERY_NOROWS(lsqlite3,
771                                              FALSE,
772                                              "UPDATE ldb_attr_%q "
773                                              "  SET attr_value = %Q "
774                                              "  WHERE eid = %lld;",
775                                              pAttrName,
776                                              el->values[j].data,
777                                              eid);
778                                 QUERY_NOROWS(lsqlite3,
779                                              FALSE,
780                                              "UPDATE ldb_entry "
781                                              "  SET entry_data = "
782                                              "        mod_attr(entry_data, "
783                                              "                 %Q, %Q) "
784                                              "  WHERE eid = %lld;",
785                                              el->name, el->values[j].data,
786                                              eid);
787                                 break;
788
789                         case LDB_FLAG_MOD_DELETE:
790                                 /* No additional parameters to this query */
791                                 QUERY_NOROWS(lsqlite3,
792                                              FALSE,
793                                              "DELETE FROM ldb_attr_%q "
794                                              "  WHERE eid = %lld "
795                                              "    AND attr_value = %Q;",
796                                              pAttrName,
797                                              eid,
798                                              el->values[j].data);
799                                 QUERY_NOROWS(lsqlite3,
800                                              FALSE,
801                                              "UPDATE ldb_entry "
802                                              "  SET entry_data = "
803                                              "        del_attr(entry_data, "
804                                              "                 %Q, %Q) "
805                                              "  WHERE eid = %lld;",
806                                              el->name, el->values[j].data,
807                                              eid);
808                                 break;
809                         }
810                 }
811         }
812
813         return 0;
814 }
815
816
817 static int
818 lsqlite3_new_dn(struct ldb_module * module,
819                 char * pDN,
820                 long long * pEID)
821 {
822         struct ldb_dn *             pExplodedDN;
823         struct ldb_context *        ldb = module->ldb;
824 //      struct lsqlite3_private *   lsqlite3 = module->private_data;
825
826         /* Explode and normalize the DN */
827         if ((pExplodedDN =
828              ldb_explode_dn(ldb,
829                             pDN,
830                             ldb,
831                             lsqlite3_case_fold_attr_required)) == NULL) {
832                 return -1;
833         }
834
835 #warning "*** lsqlite3_new_dn() not yet fully implemented ***"
836         return -1;
837 }
838
839
840 /*
841  * add a record
842  */
843 static int
844 lsqlite3_add(struct ldb_module *module,
845              const struct ldb_message *msg)
846 {
847         long long                   eid;
848         struct lsqlite3_private *   lsqlite3 = module->private_data;
849
850         /* ignore ltdb specials */
851         if (msg->dn[0] == '@') {
852                 return 0;
853         }
854
855         /* Begin a transaction */
856         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
857
858         /*
859          * Build any portions of the directory tree that don't exist.  If the
860          * final component already exists, it's an error.
861          */
862         if (lsqlite3_new_dn(module, msg->dn, &eid) != 0) {
863                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
864                 return -1;
865         }
866
867         /* Add attributes to this new entry */
868         if (lsqlite3_msg_to_sql(module, msg, eid, FALSE) != 0) {
869                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
870                 return -1;
871         }
872
873         /* Everything worked.  Commit it! */
874         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
875         return 0;
876 }
877
878
879 /*
880  * modify a record
881  */
882 static int
883 lsqlite3_modify(struct ldb_module *module,
884                 const struct ldb_message *msg)
885 {
886         struct lsqlite3_private *   lsqlite3 = module->private_data;
887
888         /* ignore ltdb specials */
889         if (msg->dn[0] == '@') {
890                 return 0;
891         }
892
893         /* Begin a transaction */
894         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
895
896         /* Everything worked.  Commit it! */
897         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
898         return 0 ;
899 }
900
901 static int
902 lsqlite3_lock(struct ldb_module *module,
903               const char *lockname)
904 {
905         if (lockname == NULL) {
906                 return -1;
907         }
908
909         /* TODO implement a local locking mechanism here */
910
911         return 0;
912 }
913
914 static int
915 lsqlite3_unlock(struct ldb_module *module,
916                 const char *lockname)
917 {
918         if (lockname == NULL) {
919                 return -1;
920         }
921
922         /* TODO implement a local locking mechanism here */
923
924         return 0;
925 }
926
927 /*
928  * return extended error information
929  */
930 static const char *
931 lsqlite3_errstring(struct ldb_module *module)
932 {
933         struct lsqlite3_private *   lsqlite3 = module->private_data;
934
935         return sqlite3_errmsg(lsqlite3->sqlite);
936 }
937
938
939 static const struct ldb_module_ops lsqlite3_ops = {
940         "sqlite",
941         lsqlite3_search,
942         lsqlite3_add,
943         lsqlite3_modify,
944         lsqlite3_delete,
945         lsqlite3_rename,
946         lsqlite3_lock,
947         lsqlite3_unlock,
948         lsqlite3_errstring
949 };
950
951
952 static int
953 lsqlite3_destructor(void *p)
954 {
955         struct lsqlite3_private *   lsqlite3 = p;
956
957         (void) sqlite3_close(lsqlite3->sqlite);
958         return 0;
959 }
960
961 static int
962 lsqlite3_initialize(struct lsqlite3_private *lsqlite3,
963                     const char *url)
964 {
965         int             ret;
966         const char *    p;
967         long long       queryInt;
968         const char *    pTail;
969         sqlite3_stmt *  stmt;
970         const char *    schema =       
971                 "-- ------------------------------------------------------"
972
973                 "PRAGMA auto_vacuum=1;"
974
975                 "-- ------------------------------------------------------"
976
977                 "BEGIN EXCLUSIVE;"
978
979                 "-- ------------------------------------------------------"
980
981                 "CREATE TABLE ldb_info AS"
982                 "  SELECT 'LDB' AS database_type,"
983                 "         '1.0' AS version;"
984
985                 "-- ------------------------------------------------------"
986                 "-- Schema"
987
988                 "/*"
989                 " * The entry table holds the information about an entry. "
990                 " * This table is used to obtain the EID of the entry and to "
991                 " * support scope=one and scope=base.  The parent and child"
992                 " * table is included in the entry table since all the other"
993                 " * attributes are dependent on EID."
994                 " */"
995                 "CREATE TABLE ldb_entry"
996                 "("
997                 "  -- Unique identifier of this LDB entry"
998                 "  eid                   INTEGER PRIMARY KEY,"
999
1000                 "  -- Unique identifier of the parent LDB entry"
1001                 "  peid                  INTEGER REFERENCES ldb_entry,"
1002
1003                 "  -- Distinguished name of this entry"
1004                 "  dn                    TEXT,"
1005
1006                 "  -- Time when the entry was created"
1007                 "  create_timestamp      INTEGER,"
1008
1009                 "  -- Time when the entry was last modified"
1010                 "  modify_timestamp      INTEGER,"
1011
1012                 "  -- Attributes of this entry, in the form"
1013                 "  --   attr\1value\0[attr\1value\0]*\0"
1014                 "  entry_data            TEXT"
1015                 ");"
1016
1017
1018                 "/*"
1019                 " * The purpose of the descendant table is to support the"
1020                 " * subtree search feature.  For each LDB entry with a unique"
1021                 " * ID (AEID), this table contains the unique identifiers"
1022                 " * (DEID) of the descendant entries."
1023                 " *"
1024                 " * For evern entry in the directory, a row exists in this"
1025                 " * table for each of its ancestors including itself.  The "
1026                 " * size of the table depends on the depth of each entry.  In "
1027                 " * the worst case, if all the entries were at the same "
1028                 " * depth, the number of rows in the table is O(nm) where "
1029                 " * n is the number of nodes in the directory and m is the "
1030                 " * depth of the tree. "
1031                 " */"
1032                 "CREATE TABLE ldb_descendants"
1033                 "("
1034                 "  -- The unique identifier of the ancestor LDB entry"
1035                 "  aeid                  INTEGER REFERENCES ldb_entry,"
1036
1037                 "  -- The unique identifier of the descendant LDB entry"
1038                 "  deid                  INTEGER REFERENCES ldb_entry"
1039                 ");"
1040
1041
1042                 "CREATE TABLE ldb_object_classes"
1043                 "("
1044                 "  -- Object classes are inserted into this table to track"
1045                 "  -- their class hierarchy.  'top' is the top-level class"
1046                 "  -- of which all other classes are subclasses."
1047                 "  class_name            TEXT PRIMARY KEY,"
1048
1049                 "  -- tree_key tracks the position of the class in"
1050                 "  -- the hierarchy"
1051                 "  tree_key              TEXT UNIQUE"
1052                 ");"
1053
1054                 "/*"
1055                 " * There is one attribute table per searchable attribute."
1056                 " */"
1057                 "/*"
1058                 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
1059                 "("
1060                 "  -- The unique identifier of the LDB entry"
1061                 "  eid                   INTEGER REFERENCES ldb_entry,"
1062
1063                 "  -- Normalized attribute value"
1064                 "  attr_value            TEXT"
1065                 ");"
1066                 "*/"
1067
1068
1069                 "-- ------------------------------------------------------"
1070                 "-- Indexes"
1071
1072
1073                 "-- ------------------------------------------------------"
1074                 "-- Triggers"
1075
1076                 "CREATE TRIGGER ldb_entry_insert_tr"
1077                 "  AFTER INSERT"
1078                 "  ON ldb_entry"
1079                 "  FOR EACH ROW"
1080                 "    BEGIN"
1081                 "      UPDATE ldb_entry"
1082                 "        SET create_timestamp = strftime('%s', 'now'),"
1083                 "            modify_timestamp = strftime('%s', 'now')"
1084                 "        WHERE eid = new.eid;"
1085                 "    END;"
1086
1087                 "CREATE TRIGGER ldb_entry_update_tr"
1088                 "  AFTER UPDATE"
1089                 "  ON ldb_entry"
1090                 "  FOR EACH ROW"
1091                 "    BEGIN"
1092                 "      UPDATE ldb_entry"
1093                 "        SET modify_timestamp = strftime('%s', 'now')"
1094                 "        WHERE eid = old.eid;"
1095                 "    END;"
1096
1097                 "-- ------------------------------------------------------"
1098                 "-- Table initialization"
1099
1100                 "/* We need an implicit 'top' level object class */"
1101                 "INSERT INTO ldb_attributes (attr_name,"
1102                 "                            parent_tree_key)"
1103                 "  SELECT 'top', '';"
1104
1105                 "-- ------------------------------------------------------"
1106
1107                 "COMMIT;"
1108
1109                 "-- ------------------------------------------------------"
1110                 ;
1111         
1112         /* Skip protocol indicator of url  */
1113         if (strncmp(url, "sqlite://", 9) != 0) {
1114                 return SQLITE_MISUSE;
1115         } else {
1116                 p = url + 9;
1117         }
1118                 
1119         /*
1120          * See if we'll be creating a new database, or opening an existing one
1121          */
1122         /* Try to open the (possibly empty/non-existent) database */
1123         if ((ret = sqlite3_open(p, &lsqlite3->sqlite)) != SQLITE_OK) {
1124                 return ret;
1125         }
1126
1127         /* Begin a transaction */
1128         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
1129
1130         /* Determine if this is a new database */
1131         QUERY_INT(lsqlite3,
1132                   queryInt,
1133                   TRUE,
1134                   "SELECT COUNT(*) "
1135                   "  FROM sqlite_master "
1136                   "  WHERE type = 'table';");
1137
1138         if (queryInt == 0) {
1139                 /*
1140                  * Create the database schema
1141                  */
1142                 for (pTail = discard_const_p(char, schema); pTail != NULL; ) {
1143
1144                         if ((ret = sqlite3_prepare(
1145                                      lsqlite3->sqlite,
1146                                      pTail,
1147                                      -1,
1148                                      &stmt,
1149                                      &pTail)) != SQLITE_OK ||
1150                             (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
1151                             (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
1152
1153                                 (void) sqlite3_close(lsqlite3->sqlite);
1154                                 return ret;
1155                         }
1156                 }
1157         } else {
1158                 /*
1159                  * Ensure that the database we opened is one of ours
1160                  */
1161                 if (lsqlite3_query_int(lsqlite3,
1162                                        &queryInt,
1163                                        "SELECT "
1164                                        "  (SELECT COUNT(*) = 3"
1165                                        "     FROM sqlite_master "
1166                                        "     WHERE type = 'table' "
1167                                        "       AND name IN "
1168                                        "         ("
1169                                        "           'ldb_entry', "
1170                                        "           'ldb_descendants', "
1171                                        "           'ldb_object_classes' "
1172                                        "         ) "
1173                                        "  ) "
1174                                        "  AND "
1175                                        "  (SELECT 1 "
1176                                        "     FROM ldb_info "
1177                                        "     WHERE database_type = 'LDB' "
1178                                        "       AND version = '1.0'"
1179                                        "  );") != 0 ||
1180                     queryInt != 1) {
1181                 
1182                         /* It's not one that we created.  See ya! */
1183                         (void) sqlite3_close(lsqlite3->sqlite);
1184                         return SQLITE_MISUSE;
1185                 }
1186         }
1187
1188         return SQLITE_OK;
1189 }
1190
1191 /*
1192  * connect to the database
1193  */
1194 struct ldb_context *
1195 lsqlite3_connect(const char *url, 
1196                  unsigned int flags, 
1197                  const char *options[])
1198 {
1199         int                         i;
1200         int                         ret;
1201         struct ldb_context *        ldb = NULL;
1202         struct lsqlite3_private *   lsqlite3 = NULL;
1203
1204         ldb = talloc(NULL, struct ldb_context);
1205         if (!ldb) {
1206                 errno = ENOMEM;
1207                 goto failed;
1208         }
1209
1210         lsqlite3 = talloc(ldb, struct lsqlite3_private);
1211         if (!lsqlite3) {
1212                 errno = ENOMEM;
1213                 goto failed;
1214         }
1215
1216         lsqlite3->sqlite = NULL;
1217         lsqlite3->options = NULL;
1218         lsqlite3->lock_count = 0;
1219
1220         ret = lsqlite3_initialize(lsqlite3, url);
1221         if (ret != SQLITE_OK) {
1222                 goto failed;
1223         }
1224
1225         talloc_set_destructor(lsqlite3, lsqlite3_destructor);
1226
1227         ldb->modules = talloc(ldb, struct ldb_module);
1228         if (!ldb->modules) {
1229                 errno = ENOMEM;
1230                 goto failed;
1231         }
1232         ldb->modules->ldb = ldb;
1233         ldb->modules->prev = ldb->modules->next = NULL;
1234         ldb->modules->private_data = lsqlite3;
1235         ldb->modules->ops = &lsqlite3_ops;
1236
1237         if (options) {
1238                 /*
1239                  * take a copy of the options array, so we don't have to rely
1240                  * on the caller keeping it around (it might be dynamic)
1241                  */
1242                 for (i=0;options[i];i++) ;
1243
1244                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
1245                 if (!lsqlite3->options) {
1246                         goto failed;
1247                 }
1248                 
1249                 for (i=0;options[i];i++) {
1250
1251                         lsqlite3->options[i+1] = NULL;
1252                         lsqlite3->options[i] =
1253                                 talloc_strdup(lsqlite3->options, options[i]);
1254                         if (!lsqlite3->options[i]) {
1255                                 goto failed;
1256                         }
1257                 }
1258         }
1259
1260         return ldb;
1261
1262 failed:
1263         if (lsqlite3->sqlite != NULL) {
1264                 (void) sqlite3_close(lsqlite3->sqlite);
1265         }
1266         talloc_free(ldb);
1267         return NULL;
1268 }
1269