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