09ba24022f2f916cc392829b362715e999eee05e
[sfrench/samba-autobuild/.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_explode_dn.h"
40 #include "ldb/ldb_sqlite3/ldb_sqlite3.h"
41
42 /*
43  * Macros used throughout
44  */
45
46 #ifndef FALSE
47 # define FALSE  (0)
48 # define TRUE   (! FALSE)
49 #endif
50
51 #define FILTER_ATTR_TABLE       "temp_filter_attrs"
52 #define RESULT_ATTR_TABLE       "temp_result_attrs"
53
54 #define QUERY_NOROWS(lsqlite3, bRollbackOnError, sql...)        \
55         do {                                                    \
56                 if (query_norows(lsqlite3, sql) != 0) {         \
57                         if (bRollbackOnError) {                 \
58                                 query_norows(lsqlite3,          \
59                                              "ROLLBACK;");      \
60                         }                                       \
61                         return -1;                              \
62                 }                                               \
63         } while (0)
64
65 #define QUERY_INT(lsqlite3, result_var, bRollbackOnError, sql...)       \
66         do {                                                            \
67                 if (query_int(lsqlite3, &result_var, sql) != 0) {       \
68                         if (bRollbackOnError) {                         \
69                                 query_norows(lsqlite3,                  \
70                                              "ROLLBACK;");              \
71                         }                                               \
72                         return -1;                                      \
73                 }                                                       \
74         } while (0)
75
76
77 /*
78  * Static variables
79  */
80 static int      lsqlite3_debug = TRUE;
81
82
83 /*
84  * Forward declarations
85  */
86 static int
87 lsqlite3_rename(struct ldb_module * module,
88                 const char * olddn,
89                 const char * newdn);
90
91 static int
92 lsqlite3_delete(struct ldb_module *module,
93                 const char *dn);
94
95 static int
96 lsqlite3_search_bytree(struct ldb_module * module,
97                        const char * pBaseDN,
98                        enum ldb_scope scope,
99                        struct ldb_parse_tree * pTree,
100                        const char * const * attrs,
101                        struct ldb_message *** pppRes);
102
103 static int
104 lsqlite3_search(struct ldb_module * module,
105                 const char * pBaseDN,
106                 enum ldb_scope scope,
107                 const char * pExpression,
108                 const char * const attrs[],
109                 struct ldb_message *** pppRes);
110
111 static int
112 lsqlite3_add(struct ldb_module *module,
113              const struct ldb_message *msg);
114
115 static int
116 lsqlite3_modify(struct ldb_module *module,
117                 const struct ldb_message *msg);
118
119 static int
120 lsqlite3_lock(struct ldb_module *module,
121               const char *lockname);
122
123 static int
124 lsqlite3_unlock(struct ldb_module *module,
125                 const char *lockname);
126
127 static const char *
128 lsqlite3_errstring(struct ldb_module *module);
129
130 static int
131 initialize(struct lsqlite3_private *lsqlite3,
132            const char *url);
133
134 static int
135 destructor(void *p);
136
137 static int
138 query_norows(const struct lsqlite3_private *lsqlite3,
139              const char *pSql,
140              ...);
141
142 static int
143 query_int(const struct lsqlite3_private * lsqlite3,
144           long long * pRet,
145           const char * pSql,
146           ...);
147
148 static int case_fold_attr_required(void * hUserData,
149                                    char *attr);
150
151 static int
152 add_msg_attr(void * hTalloc,
153              long long eid,
154              const char * pDN,
155              const char * pAttrName,
156              const char * pAttrValue,
157              long long prevEID,
158              int * pAllocated,
159              struct ldb_message *** pppRes);
160
161 static char *
162 parsetree_to_sql(struct ldb_module *module,
163                  char * hTalloc,
164                  const struct ldb_parse_tree *t);
165
166 static int
167 parsetree_to_attrlist(struct lsqlite3_private * lsqlite3,
168                       const struct ldb_parse_tree * t);
169
170 static char *
171 build_attr_table_list(void * hTalloc,
172                       struct lsqlite3_private * lsqlite3);
173
174 static int
175 msg_to_sql(struct ldb_module * module,
176            const struct ldb_message * msg,
177            long long eid,
178            int use_flags);
179
180 static int
181 new_dn(struct ldb_module * module,
182        char * pDN,
183        long long * pEID);
184
185 static int
186 new_attr(struct ldb_module * module,
187          char * pAttrName);
188
189
190 /*
191  * Table of operations for the sqlite3 backend
192  */
193 static const struct ldb_module_ops lsqlite3_ops = {
194         .name          = "sqlite",
195         .search        = lsqlite3_search,
196         .search_bytree = lsqlite3_search_bytree,
197         .add_record    = lsqlite3_add,
198         .modify_record = lsqlite3_modify,
199         .delete_record = lsqlite3_delete,
200         .rename_record = lsqlite3_rename,
201         .named_lock    = lsqlite3_lock,
202         .named_unlock  = lsqlite3_unlock,
203         .errstring     = lsqlite3_errstring
204 };
205
206
207
208
209 /*
210  * Public functions
211  */
212
213
214 /*
215  * connect to the database
216  */
217 struct ldb_context *
218 lsqlite3_connect(const char *url, 
219                  unsigned int flags, 
220                  const char *options[])
221 {
222         int                         i;
223         int                         ret;
224         struct ldb_context *        ldb = NULL;
225         struct lsqlite3_private *   lsqlite3 = NULL;
226         
227         ldb = talloc(NULL, struct ldb_context);
228         if (!ldb) {
229                 goto failed;
230         }
231         
232         lsqlite3 = talloc(ldb, struct lsqlite3_private);
233         if (!lsqlite3) {
234                 goto failed;
235         }
236         
237         lsqlite3->sqlite = NULL;
238         lsqlite3->options = NULL;
239         lsqlite3->lock_count = 0;
240         
241         ret = initialize(lsqlite3, url);
242         if (ret != SQLITE_OK) {
243                 goto failed;
244         }
245         
246         talloc_set_destructor(lsqlite3, destructor);
247         
248         ldb->modules = talloc(ldb, struct ldb_module);
249         if (!ldb->modules) {
250                 goto failed;
251         }
252         ldb->modules->ldb = ldb;
253         ldb->modules->prev = ldb->modules->next = NULL;
254         ldb->modules->private_data = lsqlite3;
255         ldb->modules->ops = &lsqlite3_ops;
256         
257         if (options) {
258                 /*
259                  * take a copy of the options array, so we don't have to rely
260                  * on the caller keeping it around (it might be dynamic)
261                  */
262                 for (i=0;options[i];i++) ;
263                 
264                 lsqlite3->options = talloc_array(lsqlite3, char *, i+1);
265                 if (!lsqlite3->options) {
266                         goto failed;
267                 }
268                 
269                 for (i=0;options[i];i++) {
270                         
271                         lsqlite3->options[i+1] = NULL;
272                         lsqlite3->options[i] =
273                                 talloc_strdup(lsqlite3->options, options[i]);
274                         if (!lsqlite3->options[i]) {
275                                 goto failed;
276                         }
277                 }
278         }
279         
280         return ldb;
281         
282 failed:
283         if (lsqlite3->sqlite != NULL) {
284                 (void) sqlite3_close(lsqlite3->sqlite);
285         }
286         talloc_free(ldb);
287         return NULL;
288 }
289
290
291 /*
292  * Interface functions referenced by lsqlite3_ops
293  */
294
295 /* rename a record */
296 static int
297 lsqlite3_rename(struct ldb_module * module,
298                 const char * olddn,
299                 const char * newdn)
300 {
301         /* ignore ltdb specials */
302         if (olddn[0] == '@' ||newdn[0] == '@') {
303                 return 0;
304         }
305         
306 #warning "lsqlite3_rename() is not yet supported"
307
308         return -1;
309 }
310
311 /* delete a record */
312 static int
313 lsqlite3_delete(struct ldb_module *module,
314                 const char *dn)
315 {
316         struct lsqlite3_private *   lsqlite3 = module->private_data;
317
318         /* ignore ltdb specials */
319         if (dn[0] == '@') {
320                 return 0;
321         }
322         
323         /* Begin a transaction */
324         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
325
326
327 #warning "lsqlite3_delete() is not yet supported"
328
329         /* Commit the transaction */
330         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
331         
332         return 0;
333 }
334
335 /* search for matching records, by tree */
336 static int
337 lsqlite3_search_bytree(struct ldb_module * module,
338                        const char * pBaseDN,
339                        enum ldb_scope scope,
340                        struct ldb_parse_tree * pTree,
341                        const char * const * attrs,
342                        struct ldb_message *** pppRes)
343 {
344         int                         ret;
345         int                         allocated;
346         int                         bLoop;
347         long long                   eid = 0;
348         long long                   prevEID;
349         char *                      pSql = NULL;
350         char *                      pSqlConstraints;
351         char *                      pTableList;
352         char *                      hTalloc = NULL;
353         const char *                pDN;
354         const char *                pAttrName;
355         const char *                pAttrValue;
356         const char *                pResultAttrList;
357         const char * const *        pRequestedAttrs;
358         sqlite3_stmt *              pStmt;
359         struct lsqlite3_private *   lsqlite3 = module->private_data;
360         
361         if (pBaseDN == NULL) {
362                 pBaseDN = "";
363         }
364         
365         /* Begin a transaction */
366         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN IMMEDIATE;");
367         
368         /*
369          * Obtain the eid of the base DN
370          */
371         if ((ret = query_int(lsqlite3,
372                              &eid,
373                              "SELECT eid\n"
374                              "  FROM ldb_attr_DN\n"
375                              "  WHERE attr_value = %Q;",
376                              pBaseDN)) == SQLITE_DONE) {
377                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
378                 return 0;
379         } else if (ret != SQLITE_OK) {
380                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
381                 return -1;
382         }
383         
384         /* Allocate a temporary talloc context */
385         if ((hTalloc = talloc_new(module->ldb)) == NULL) {
386                 ret = -1;
387                 talloc_free(pTree);
388                 goto cleanup;
389         }
390         
391         /* Convert filter into a series of SQL conditions (constraints) */
392         pSqlConstraints = parsetree_to_sql(module, hTalloc, pTree);
393         
394         /* Ensure we're starting with an empty result attribute table */
395         QUERY_NOROWS(lsqlite3,
396                      FALSE,
397                      "DELETE FROM " RESULT_ATTR_TABLE "\n"
398                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
399         
400         /* Initially, we don't know what the requested attributes are */
401         if (attrs == NULL) {
402                 /* but they didn't give us any so we'll retrieve all of 'em */
403                 pResultAttrList = "";
404         } else {
405                 /* Discover the list of attributes */
406                 pResultAttrList = NULL;
407         }
408
409         /* Insert the list of requested attributes into this table */
410         for (pRequestedAttrs = (const char * const *) attrs;
411              pRequestedAttrs != NULL && *pRequestedAttrs != NULL;
412              pRequestedAttrs++) {
413                 
414                 /* If any attribute in the list is "*" then... */
415                 if (strcmp(*pRequestedAttrs, "*") == 0) {
416                         /* we want all attribute types */
417                         pResultAttrList = "";
418                         break;
419                         
420                 } else {
421                         /* otherwise, add this name to the resuult list */
422                         QUERY_NOROWS(lsqlite3,
423                                      FALSE,
424                                      "INSERT OR IGNORE\n"
425                                      "  INTO " RESULT_ATTR_TABLE "\n"
426                                      "    (attr_name)\n"
427                                      "  VALUES\n"
428                                      "    (%Q);",
429                                      *pRequestedAttrs);
430                 }
431         }
432         
433         /* If we didn't get a "*" for all attributes in the result list... */
434         if (pResultAttrList == NULL) {
435                 /* ... then we'll use the result attribute table */
436                 pResultAttrList =
437                         "    AND av.attr_name IN\n"
438                         "          (SELECT attr_name\n"
439                         "             FROM " RESULT_ATTR_TABLE ") ";
440         }
441
442         /* Ensure we're starting with an empty filter attribute table */
443         QUERY_NOROWS(lsqlite3,
444                      FALSE,
445                      "DELETE FROM " FILTER_ATTR_TABLE "\n"
446                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
447         
448         /*
449          * Create a table of unique attribute names for our extra table list
450          */
451         if ((ret = parsetree_to_attrlist(lsqlite3, pTree)) != 0) {
452                 ret = -1;
453                 goto cleanup;
454         }
455         
456         /*
457          * Build the attribute table list from the list of unique names.
458          */
459         
460         if ((pTableList = build_attr_table_list(hTalloc, lsqlite3)) == NULL) {
461                 ret = -1;
462                 goto cleanup;
463         }
464         
465         switch(scope) {
466         case LDB_SCOPE_DEFAULT:
467         case LDB_SCOPE_SUBTREE:
468                 pSql = sqlite3_mprintf(
469                         "SELECT entry.eid,\n"
470                         "       entry.dn,\n"
471                         "       av.attr_name,\n"
472                         "       av.attr_value\n"
473                         "  FROM ldb_entry AS entry,\n"
474                         "       ldb_attribute_values AS av\n"
475                         "  WHERE entry.eid IN\n"
476                         "    (SELECT DISTINCT ldb_entry.eid\n"
477                         "       FROM ldb_entry,\n"
478                         "            ldb_descendants\n"
479                         "       WHERE ldb_descendants.aeid = %lld\n"
480                         "         AND ldb_entry.eid = ldb_descendants.deid\n"
481                         "         AND ldb_entry.eid IN\n%s\n"
482                         "    )\n"
483                         "    AND av.eid = entry.eid\n"
484                         "    %s\n"
485                         "  ORDER BY av.eid, av.attr_name;",
486                         eid,
487                         pSqlConstraints,
488                         pResultAttrList);
489                 break;
490                 
491         case LDB_SCOPE_BASE:
492                 pSql = sqlite3_mprintf(
493                         "SELECT entry.eid,\n"
494                         "       entry.dn,\n"
495                         "       av.attr_name,\n"
496                         "       av.attr_value\n"
497                         "  FROM ldb_entry AS entry,\n"
498                         "       ldb_attribute_values AS av\n"
499                         "  WHERE entry.eid IN\n"
500                         "    (SELECT DISTINCT ldb_entry.eid\n"
501                         "       FROM ldb_entry\n"
502                         "       WHERE ldb_entry.eid = %lld\n"
503                         "         AND ldb_entry.eid IN\n%s\n"
504                         "    )\n"
505                         "    AND av.eid = entry.eid\n"
506                         "    %s\n"
507                         "  ORDER BY av.eid, av.attr_name;",
508                         eid,
509                         pSqlConstraints,
510                         pResultAttrList);
511                 break;
512                 
513         case LDB_SCOPE_ONELEVEL:
514                 pSql = sqlite3_mprintf(
515                         "SELECT entry.eid,\n"
516                         "       entry.dn,\n"
517                         "       av.attr_name,\n"
518                         "       av.attr_value\n"
519                         "  FROM ldb_entry AS entry,\n"
520                         "       ldb_attribute_values AS av\n"
521                         "  WHERE entry.eid IN\n"
522                         "    (SELECT DISTINCT ldb_entry.eid\n"
523                         "       FROM ldb_entry AS pchild\n"
524                         "       WHERE ldb_entry.eid = pchild.eid\n"
525                         "         AND pchild.peid = %lld\n"
526                         "         AND ldb_entry.eid IN\n%s\n"
527                         "    )\n"
528                         "    AND av.eid = entry.eid\n"
529                         "    %s\n"
530                         "  ORDER BY av.eid, av.attr_name;\n",
531                         eid,
532                         pSqlConstraints,
533                         pResultAttrList);
534                 break;
535         }
536         
537         if (lsqlite3_debug) {
538                 printf("%s\n", pSql);
539         }
540
541         /*
542          * Prepare and execute the SQL statement.  Loop allows retrying on
543          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
544          * requiring retrying the operation.
545          */
546         for (bLoop = TRUE; bLoop; ) {
547                 /* There are no allocate message structures yet */
548                 allocated = 0;
549                 if (pppRes != NULL) {
550                         *pppRes = NULL;
551                 }
552                 
553                 /* Compile the SQL statement into sqlite virtual machine */
554                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
555                                            pSql,
556                                            -1,
557                                            &pStmt,
558                                            NULL)) == SQLITE_SCHEMA) {
559                         if (pppRes != NULL && *pppRes != NULL) {
560                                 talloc_free(*pppRes);
561                         }
562                         continue;
563                 } else if (ret != SQLITE_OK) {
564                         ret = -1;
565                         break;
566                 }
567                 
568                 /* Initially, we have no previous eid */
569                 prevEID = -1;
570                 
571                 /* Loop through the returned rows */
572                 for (ret = SQLITE_ROW; ret == SQLITE_ROW; ) {
573                         
574                         /* Get the next row */
575                         if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
576                                 
577                                 /* Get the values from this row */
578                                 eid = sqlite3_column_int64(pStmt, 0);
579                                 pDN = sqlite3_column_text(pStmt, 1);
580                                 pAttrName = sqlite3_column_text(pStmt, 2);
581                                 pAttrValue = sqlite3_column_text(pStmt, 3);
582                                 
583                                 /* Add this result to the result set */
584                                 if ((ret = add_msg_attr(hTalloc,
585                                                         eid,
586                                                         pDN,
587                                                         pAttrName,
588                                                         pAttrValue,
589                                                         prevEID,
590                                                         &allocated,
591                                                         pppRes)) != 0) {
592                                         
593                                         (void) sqlite3_finalize(pStmt);
594                                         ret = -1;
595                                         break;
596                                 }
597                         }
598                 }
599                 
600                 if (ret == SQLITE_SCHEMA) {
601                         (void) sqlite3_finalize(pStmt);
602                         if (pppRes != NULL && *pppRes != NULL) {
603                                 talloc_free(*pppRes);
604                         }
605                         continue;
606                 } else if (ret != SQLITE_DONE) {
607                         (void) sqlite3_finalize(pStmt);
608                         ret = -1;
609                         break;
610                 }
611                 
612                 /* Free the virtual machine */
613                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
614                         (void) sqlite3_finalize(pStmt);
615                         if (pppRes != NULL && *pppRes != NULL) {
616                                 talloc_free(*pppRes);
617                         }
618                         continue;
619                 } else if (ret != SQLITE_OK) {
620                         (void) sqlite3_finalize(pStmt);
621                         ret = -1;
622                         break;
623                 }
624                 
625                 /*
626                  * Normal condition is only one time through loop.  Loop is
627                  * rerun in error conditions, via "continue", above.
628                  */
629                 ret = 0;
630                 bLoop = FALSE;
631         }
632         
633         /* End the transaction */
634         QUERY_NOROWS(lsqlite3, FALSE, "END TRANSACTION;");
635         
636         /* We're alll done with this query */
637         sqlite3_free(pSql);
638         
639         /* Were there any results? */
640         if (ret != 0 || allocated == 0) {
641                 /* Nope.  We can free the results. */
642                 if (pppRes != NULL && *pppRes != NULL) {
643                         talloc_free(*pppRes);
644                 }
645         }
646         
647 cleanup:
648         /* Clean up our temporary tables */
649         QUERY_NOROWS(lsqlite3,
650                      FALSE,
651                      "DELETE FROM " RESULT_ATTR_TABLE "\n"
652                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
653         
654         QUERY_NOROWS(lsqlite3,
655                      FALSE,
656                      "DELETE FROM " FILTER_ATTR_TABLE "\n"
657                      "  WHERE 1;");/* avoid a schema change with WHERE 1 */
658         
659         
660         if (hTalloc != NULL) {
661                 talloc_free(hTalloc);
662         }
663         
664         /* If error, return error code; otherwise return number of results */
665         return ret == 0 ? allocated : ret;
666 }
667
668 /* search for matching records, by expression */
669 static int
670 lsqlite3_search(struct ldb_module * module,
671                 const char * pBaseDN,
672                 enum ldb_scope scope,
673                 const char * pExpression,
674                 const char * const * attrs,
675                 struct ldb_message *** pppRes)
676 {
677         int                     ret;
678         struct ldb_parse_tree * pTree;
679         
680         /* Parse the filter expression into a tree we can work with */
681         if ((pTree = ldb_parse_tree(module->ldb, pExpression)) == NULL) {
682                 return -1;
683         }
684         
685         /* Now use the bytree function for the remainder of processing */
686         ret = lsqlite3_search_bytree(module, pBaseDN, scope,
687                                      pTree, attrs, pppRes);
688         
689         /* Free the parse tree */
690         talloc_free(pTree);
691         
692         /* All done. */
693         return ret;
694 }
695
696
697 /* add a record */
698 static int
699 lsqlite3_add(struct ldb_module *module,
700              const struct ldb_message *msg)
701 {
702         long long                   eid;
703         struct lsqlite3_private *   lsqlite3 = module->private_data;
704         
705         /* ignore ltdb specials */
706         if (msg->dn[0] == '@') {
707                 return 0;
708         }
709         
710         /* Begin a transaction */
711         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
712         
713         /*
714          * Build any portions of the directory tree that don't exist.  If the
715          * final component already exists, it's an error.
716          */
717         if (new_dn(module, msg->dn, &eid) != 0) {
718                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
719                 return -1;
720         }
721         
722         /* Add attributes to this new entry */
723         if (msg_to_sql(module, msg, eid, FALSE) != 0) {
724                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
725                 return -1;
726         }
727         
728         /* Everything worked.  Commit it! */
729         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
730         return 0;
731 }
732
733
734 /* modify a record */
735 static int
736 lsqlite3_modify(struct ldb_module *module,
737                 const struct ldb_message *msg)
738 {
739         struct lsqlite3_private *   lsqlite3 = module->private_data;
740         
741         /* ignore ltdb specials */
742         if (msg->dn[0] == '@') {
743                 return 0;
744         }
745         
746         /* Begin a transaction */
747         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
748         
749 #warning "modify() not yet implemented"
750
751         /* Everything worked.  Commit it! */
752         QUERY_NOROWS(lsqlite3, TRUE, "COMMIT;");
753         return 0 ;
754 }
755
756 /* obtain a named lock */
757 static int
758 lsqlite3_lock(struct ldb_module *module,
759               const char *lockname)
760 {
761         if (lockname == NULL) {
762                 return -1;
763         }
764         
765         /* TODO implement a local locking mechanism here */
766         
767         return 0;
768 }
769
770 /* release a named lock */
771 static int
772 lsqlite3_unlock(struct ldb_module *module,
773                 const char *lockname)
774 {
775         if (lockname == NULL) {
776                 return -1;
777         }
778         
779         /* TODO implement a local locking mechanism here */
780         
781         return 0;
782 }
783
784 /* return extended error information */
785 static const char *
786 lsqlite3_errstring(struct ldb_module *module)
787 {
788         struct lsqlite3_private *   lsqlite3 = module->private_data;
789         
790         return sqlite3_errmsg(lsqlite3->sqlite);
791 }
792
793
794
795
796 /*
797  * Static functions
798  */
799
800 static int
801 initialize(struct lsqlite3_private *lsqlite3,
802            const char *url)
803 {
804         int             ret;
805         long long       queryInt;
806         const char *    pTail;
807         sqlite3_stmt *  stmt;
808         const char *    schema =       
809                 
810                 
811                 "CREATE TABLE ldb_info AS "
812                 "  SELECT 'LDB' AS database_type,"
813                 "         '1.0' AS version;"
814                 
815                 /*
816                  * The entry table holds the information about an entry. 
817                  * This table is used to obtain the EID of the entry and to 
818                  * support scope=one and scope=base.  The parent and child
819                  * table is included in the entry table since all the other
820                  * attributes are dependent on EID.
821                  */
822                 "CREATE TABLE ldb_entry "
823                 "("
824                 "  eid                   INTEGER PRIMARY KEY,"
825                 "  peid                  INTEGER REFERENCES ldb_entry,"
826                 "  dn                    TEXT UNIQUE,"
827                 "  create_timestamp      INTEGER,"
828                 "  modify_timestamp      INTEGER"
829                 ");"
830                 
831
832                 /*
833                  * The purpose of the descendant table is to support the
834                  * subtree search feature.  For each LDB entry with a unique
835                  * ID (AEID), this table contains the unique identifiers
836                  * (DEID) of the descendant entries.
837                  *
838                  * For evern entry in the directory, a row exists in this
839                  * table for each of its ancestors including itself.  The 
840                  * size of the table depends on the depth of each entry.  In 
841                  * the worst case, if all the entries were at the same 
842                  * depth, the number of rows in the table is O(nm) where 
843                  * n is the number of nodes in the directory and m is the 
844                  * depth of the tree. 
845                  */
846                 "CREATE TABLE ldb_descendants "
847                 "( "
848                 "  aeid                  INTEGER REFERENCES ldb_entry,"
849                 "  deid                  INTEGER REFERENCES ldb_entry"
850                 ");"
851                 
852                 
853                 "CREATE TABLE ldb_object_classes"
854                 "("
855                 "  class_name            TEXT PRIMARY KEY,"
856                 "  tree_key              TEXT UNIQUE"
857                 ");"
858                 
859                 /*
860                  * We keep a full listing of attribute/value pairs here
861                  */
862                 "CREATE TABLE ldb_attribute_values"
863                 "("
864                 "  eid                   INTEGER REFERENCES ldb_entry,"
865                 "  attr_name             TEXT,"
866                 "  attr_value            TEXT"
867                 ");"
868                 
869                 /*
870                  * There is one attribute table per searchable attribute.
871                  */
872                 /*
873                 "CREATE TABLE ldb_attr_ATTRIBUTE_NAME"
874                 "("
875                 "  eid                   INTEGER REFERENCES ldb_entry,"
876                 "  attr_value            TEXT"
877                 ");"
878                 */
879                 
880                 /*
881                  * We pre-create the dn attribute table
882                  */
883                 "CREATE TABLE ldb_attr_DN"
884                 "("
885                 "  eid                   INTEGER REFERENCES ldb_entry,"
886                 "  attr_value            TEXT"
887                 ");"
888
889                 
890                 /*
891                  * We pre-create the objectclass attribute table
892                  */
893                 "CREATE TABLE ldb_attr_OBJECTCLASS"
894                 "("
895                 "  eid                   INTEGER REFERENCES ldb_entry,"
896                 "  attr_value            TEXT"
897                 ");"
898
899                 
900                 /*
901                  * Indexes
902                  */
903                 
904                 
905                 /*
906                  * Triggers
907                  */
908                 
909                 "CREATE TRIGGER ldb_entry_insert_tr"
910                 "  AFTER INSERT"
911                 "  ON ldb_entry"
912                 "  FOR EACH ROW"
913                 "    BEGIN"
914                 "      UPDATE ldb_entry"
915                 "        SET create_timestamp = strftime('%s', 'now'),"
916                 "            modify_timestamp = strftime('%s', 'now')"
917                 "        WHERE eid = new.eid;"
918                 "    END;"
919                 
920                 "CREATE TRIGGER ldb_entry_update_tr"
921                 "  AFTER UPDATE"
922                 "  ON ldb_entry"
923                 "  FOR EACH ROW"
924                 "    BEGIN"
925                 "      UPDATE ldb_entry"
926                 "        SET modify_timestamp = strftime('%s', 'now')"
927                 "        WHERE eid = old.eid;"
928                 "    END;"
929                 
930                 /*
931                  * Table initialization
932                  */
933
934                 /* The root node */
935                 "INSERT INTO ldb_entry "
936                 "    (eid, peid, dn) "
937                 "  VALUES "
938                 "    (0, NULL, '');"
939
940                 /* And the root node "dn" attribute */
941                 "INSERT INTO ldb_attr_DN "
942                 "    (eid, attr_value) "
943                 "  VALUES "
944                 "    (0, '');"
945
946                 ;
947         
948         /* Skip protocol indicator of url  */
949         if (strncmp(url, "sqlite://", 9) != 0) {
950                 return SQLITE_MISUSE;
951         }
952         
953         /* Update pointer to just after the protocol indicator */
954         url += 9;
955         
956         /* Try to open the (possibly empty/non-existent) database */
957         if ((ret = sqlite3_open(url, &lsqlite3->sqlite)) != SQLITE_OK) {
958                 return ret;
959         }
960         
961         /* In case this is a new database, enable auto_vacuum */
962         QUERY_NOROWS(lsqlite3, FALSE, "PRAGMA auto_vacuum=1;");
963         
964         /* Begin a transaction */
965         QUERY_NOROWS(lsqlite3, FALSE, "BEGIN EXCLUSIVE;");
966         
967         /* Determine if this is a new database.  No tables means it is. */
968         QUERY_INT(lsqlite3,
969                   queryInt,
970                   TRUE,
971                   "SELECT COUNT(*)\n"
972                   "  FROM sqlite_master\n"
973                   "  WHERE type = 'table';");
974         
975         if (queryInt == 0) {
976                 /*
977                  * Create the database schema
978                  */
979                 for (pTail = discard_const_p(char, schema);
980                      pTail != NULL && *pTail != '\0';
981                         ) {
982                         
983                         if (lsqlite3_debug) {
984                                 printf("Execute first query in:\n%s\n", pTail);
985                         }
986                         
987                         if ((ret = sqlite3_prepare(
988                                      lsqlite3->sqlite,
989                                      pTail,
990                                      -1,
991                                      &stmt,
992                                      &pTail)) != SQLITE_OK ||
993                             (ret = sqlite3_step(stmt)) != SQLITE_DONE ||
994                             (ret = sqlite3_finalize(stmt)) != SQLITE_OK) {
995                                 
996                                 QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
997                                 (void) sqlite3_close(lsqlite3->sqlite);
998                                 return ret;
999                         }
1000                 }
1001         } else {
1002                 /*
1003                  * Ensure that the database we opened is one of ours
1004                  */
1005                 if (query_int(lsqlite3,
1006                               &queryInt,
1007                               "SELECT "
1008                               "  (SELECT COUNT(*) = 3"
1009                               "     FROM sqlite_master "
1010                               "     WHERE type = 'table' "
1011                               "       AND name IN "
1012                               "         ("
1013                               "           'ldb_entry', "
1014                               "           'ldb_descendants', "
1015                               "           'ldb_object_classes' "
1016                               "         ) "
1017                               "  ) "
1018                               "  AND "
1019                               "  (SELECT 1 "
1020                               "     FROM ldb_info "
1021                               "     WHERE database_type = 'LDB' "
1022                               "       AND version = '1.0'"
1023                               "  );") != 0 ||
1024                     queryInt != 1) {
1025                         
1026                         /* It's not one that we created.  See ya! */
1027                         QUERY_NOROWS(lsqlite3, FALSE, "ROLLBACK;");
1028                         (void) sqlite3_close(lsqlite3->sqlite);
1029                         return SQLITE_MISUSE;
1030                 }
1031         }
1032         
1033         /*
1034          * Create a temporary table to hold attributes requested in the result
1035          * set of a search.
1036          */
1037         QUERY_NOROWS(lsqlite3,
1038                      FALSE,
1039                      "CREATE TEMPORARY TABLE " RESULT_ATTR_TABLE "\n"
1040                      " (\n"
1041                      "  attr_name TEXT PRIMARY KEY\n"
1042                      " );");
1043         
1044         /*
1045          * Create a temporary table to hold the attributes used by filters
1046          * during a search.
1047          */
1048         QUERY_NOROWS(lsqlite3,
1049                      FALSE,
1050                      "CREATE TEMPORARY TABLE " FILTER_ATTR_TABLE "\n"
1051                      " (\n"
1052                      "  attr_name TEXT PRIMARY KEY\n"
1053                      " );");
1054         
1055         /* Commit the transaction */
1056         QUERY_NOROWS(lsqlite3, FALSE, "COMMIT;");
1057         
1058         return SQLITE_OK;
1059 }
1060
1061 static int
1062 destructor(void *p)
1063 {
1064         struct lsqlite3_private *   lsqlite3 = p;
1065         
1066         (void) sqlite3_close(lsqlite3->sqlite);
1067         return 0;
1068 }
1069
1070
1071 /*
1072  * query_norows()
1073  *
1074  * This function is used for queries that are not expected to return any rows,
1075  * e.g. BEGIN, COMMIT, ROLLBACK, CREATE TABLE, INSERT, UPDATE, DELETE, etc.
1076  * There are no provisions here for returning data from rows in a table, so do
1077  * not pass SELECT queries to this function.
1078  */
1079 static int
1080 query_norows(const struct lsqlite3_private *lsqlite3,
1081              const char *pSql,
1082              ...)
1083 {
1084         int             ret;
1085         int             bLoop;
1086         char *          p;
1087         sqlite3_stmt *  pStmt;
1088         va_list         args;
1089         
1090         /* Begin access to variable argument list */
1091         va_start(args, pSql);
1092         
1093         /* Format the query */
1094         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1095                 return -1;
1096         }
1097         
1098         if (lsqlite3_debug) {
1099                 printf("%s\n", p);
1100         }
1101
1102         /*
1103          * Prepare and execute the SQL statement.  Loop allows retrying on
1104          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1105          * requiring retrying the operation.
1106          */
1107         for (bLoop = TRUE; bLoop; ) {
1108                 
1109                 /* Compile the SQL statement into sqlite virtual machine */
1110                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1111                                            p,
1112                                            -1,
1113                                            &pStmt,
1114                                            NULL)) == SQLITE_SCHEMA) {
1115                         continue;
1116                 } else if (ret != SQLITE_OK) {
1117                         ret = -1;
1118                         break;
1119                 }
1120                 
1121                 /* No rows expected, so just step through machine code once */
1122                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1123                         (void) sqlite3_finalize(pStmt);
1124                         continue;
1125                 } else if (ret != SQLITE_DONE) {
1126                         (void) sqlite3_finalize(pStmt);
1127                         ret = -1;
1128                         break;
1129                 }
1130                 
1131                 /* Free the virtual machine */
1132                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1133                         (void) sqlite3_finalize(pStmt);
1134                         continue;
1135                 } else if (ret != SQLITE_OK) {
1136                         (void) sqlite3_finalize(pStmt);
1137                         ret = -1;
1138                         break;
1139                 }
1140                 
1141                 /*
1142                  * Normal condition is only one time through loop.  Loop is
1143                  * rerun in error conditions, via "continue", above.
1144                  */
1145                 ret = 0;
1146                 bLoop = FALSE;
1147         }
1148         
1149         /* All done with variable argument list */
1150         va_end(args);
1151         
1152         /* Free the memory we allocated for our query string */
1153         sqlite3_free(p);
1154         
1155         return ret;
1156 }
1157
1158
1159 /*
1160  * query_int()
1161  *
1162  * This function is used for the common case of queries that return a single
1163  * integer value.
1164  *
1165  * NOTE: If more than one value is returned by the query, all but the first
1166  * one will be ignored.
1167  */
1168 static int
1169 query_int(const struct lsqlite3_private * lsqlite3,
1170           long long * pRet,
1171           const char * pSql,
1172           ...)
1173 {
1174         int             ret;
1175         int             bLoop;
1176         char *          p;
1177         sqlite3_stmt *  pStmt;
1178         va_list         args;
1179         
1180         /* Begin access to variable argument list */
1181         va_start(args, pSql);
1182         
1183         /* Format the query */
1184         if ((p = sqlite3_vmprintf(pSql, args)) == NULL) {
1185                 return SQLITE_NOMEM;
1186         }
1187         
1188         if (lsqlite3_debug) {
1189                 printf("%s\n", p);
1190         }
1191
1192         /*
1193          * Prepare and execute the SQL statement.  Loop allows retrying on
1194          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1195          * requiring retrying the operation.
1196          */
1197         for (bLoop = TRUE; bLoop; ) {
1198                 
1199                 /* Compile the SQL statement into sqlite virtual machine */
1200                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1201                                            p,
1202                                            -1,
1203                                            &pStmt,
1204                                            NULL)) == SQLITE_SCHEMA) {
1205                         continue;
1206                 } else if (ret != SQLITE_OK) {
1207                         break;
1208                 }
1209                 
1210                 /* One row expected */
1211                 if ((ret = sqlite3_step(pStmt)) == SQLITE_SCHEMA) {
1212                         (void) sqlite3_finalize(pStmt);
1213                         continue;
1214                 } else if (ret != SQLITE_ROW) {
1215                         (void) sqlite3_finalize(pStmt);
1216                         break;
1217                 }
1218                 
1219                 /* Get the value to be returned */
1220                 *pRet = sqlite3_column_int64(pStmt, 0);
1221                 
1222                 /* Free the virtual machine */
1223                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1224                         (void) sqlite3_finalize(pStmt);
1225                         continue;
1226                 } else if (ret != SQLITE_OK) {
1227                         (void) sqlite3_finalize(pStmt);
1228                         break;
1229                 }
1230                 
1231                 /*
1232                  * Normal condition is only one time through loop.  Loop is
1233                  * rerun in error conditions, via "continue", above.
1234                  */
1235                 bLoop = FALSE;
1236         }
1237         
1238         /* All done with variable argument list */
1239         va_end(args);
1240         
1241         /* Free the memory we allocated for our query string */
1242         sqlite3_free(p);
1243         
1244         return ret;
1245 }
1246
1247
1248 /*
1249   callback function used in call to ldb_dn_fold() for determining whether an
1250   attribute type requires case folding.
1251 */
1252 static int
1253 case_fold_attr_required(void * hUserData,
1254                         char *attr)
1255 {
1256 //        struct ldb_module * module = hUserData;
1257         
1258 #warning "currently, all attributes require case folding"
1259         return TRUE;
1260 }
1261
1262
1263 /*
1264  * add a single set of ldap message values to a ldb_message
1265  */
1266
1267 static int
1268 add_msg_attr(void * hTalloc,
1269              long long eid,
1270              const char * pDN,
1271              const char * pAttrName,
1272              const char * pAttrValue,
1273              long long prevEID,
1274              int * pAllocated,
1275              struct ldb_message *** pppRes)
1276 {
1277         void *                       x;
1278         struct ldb_message *         msg;
1279         struct ldb_message_element * el;
1280         
1281         /* Is this a different EID than the previous one? */
1282         if (eid != prevEID) {
1283                 /* Yup.  Add another result to the result array */
1284                 if ((x = talloc_realloc(hTalloc,
1285                                         *pAllocated == 0 ? NULL : pppRes,
1286                                         struct ldb_message *,
1287                                         *pAllocated + 1)) == NULL) {
1288                         
1289                         return -1;
1290                 }
1291                 
1292                 /* Save the new result list */
1293                 *pppRes = x;
1294                 
1295                 /* We've allocated one more result */
1296                 *pAllocated++;
1297                 
1298                 /* Ensure that the message is initialized */
1299                 msg = x;
1300                 msg->dn = NULL;
1301                 msg->num_elements = 0;
1302                 msg->elements = NULL;
1303                 msg->private_data = NULL;
1304         } else {
1305                 /* Same EID.  Point to the previous most-recent message */
1306                 msg = *pppRes[*pAllocated - 1];
1307         }
1308         
1309         /*
1310          * Point to the most recent previous element.  (If there are none,
1311          * this will point to non-allocated memory, but the pointer will never
1312          * be dereferenced.)
1313          */
1314         el = &msg->elements[msg->num_elements - 1];
1315         
1316         /* See if the most recent previous element has the same attr_name */
1317         if (msg->num_elements == 0 || strcmp(el->name, pAttrName) != 0) {
1318                 
1319                 /* It's a new attr_name.  Allocate another message element */
1320                 if ((el = talloc_realloc(msg,
1321                                          msg->elements,
1322                                          struct ldb_message_element, 
1323                                          msg->num_elements + 1)) == NULL) {
1324                         return -1;
1325                 }
1326                 
1327                 /* Save the new element */
1328                 msg->elements = el;
1329                 
1330                 /* There's now one additional element */
1331                 msg->num_elements++;
1332                 
1333                 /* Save the attribute name */
1334                 if ((el->name =
1335                      talloc_strdup(msg->elements, pAttrName)) == NULL) {
1336                         
1337                         return -1;
1338                 }
1339                 
1340                 /* No flags */
1341                 el->flags = 0;
1342                 
1343                 /* Initialize number of attribute values for this type */
1344                 el->num_values = 0;
1345                 el->values = NULL;
1346         }
1347         
1348         /* Increase the value array size by 1 */
1349         if ((el->values =
1350              talloc_realloc(el,
1351                             el->num_values == 0 ? NULL : el->values,
1352                             struct ldb_val,
1353                             el->num_values)) == NULL) {
1354                 return -1;
1355         }
1356         
1357         /* Save the new attribute value length */
1358         el->values[el->num_values].length = strlen(pAttrValue) + 1;
1359         
1360         /* Copy the new attribute value */
1361         if (talloc_memdup(el->values[el->num_values].data,
1362                           pAttrValue,
1363                           el->values[el->num_values].length) == NULL) {
1364                 return -1;
1365         }
1366         
1367         /* We now have one additional value of this type */
1368         el->num_values++;
1369         
1370         return 0;
1371 }
1372
1373 static char *
1374 parsetree_to_sql(struct ldb_module *module,
1375                  char * hTalloc,
1376                  const struct ldb_parse_tree *t)
1377 {
1378         int                     i;
1379         char *                  child;
1380         char *                  p;
1381         char *                  ret = NULL;
1382         char *                  pAttrName;
1383         
1384         
1385         switch(t->operation) {
1386         case LDB_OP_SIMPLE:
1387                 break;
1388                 
1389         case LDB_OP_EXTENDED:
1390 #warning  "work out how to handle bitops"
1391                 return NULL;
1392
1393         case LDB_OP_AND:
1394                 ret = parsetree_to_sql(module,
1395                                        hTalloc,
1396                                        t->u.list.elements[0]);
1397                 
1398                 for (i = 1; i < t->u.list.num_elements; i++) {
1399                         child =
1400                                 parsetree_to_sql(
1401                                         module,
1402                                         hTalloc,
1403                                         t->u.list.elements[i]);
1404                         ret = talloc_asprintf_append(ret,
1405                                                      "INTERSECT\n"
1406                                                      "%s\n",
1407                                                      child);
1408                         talloc_free(child);
1409                 }
1410                 
1411                 child = ret;
1412                 ret = talloc_asprintf("(\n"
1413                                       "%s\n"
1414                                       ")\n",
1415                                       child);
1416                 talloc_free(child);
1417                 return ret;
1418                 
1419         case LDB_OP_OR:
1420                 child =
1421                         parsetree_to_sql(
1422                                 module,
1423                                 hTalloc,
1424                                 t->u.list.elements[0]);
1425                 
1426                 for (i = 1; i < t->u.list.num_elements; i++) {
1427                         child =
1428                                 parsetree_to_sql(
1429                                         module,
1430                                         hTalloc,
1431                                         t->u.list.elements[i]);
1432                         ret = talloc_asprintf_append(ret,
1433                                                      "UNION\n"
1434                                                      "%s\n",
1435                                                      child);
1436                         talloc_free(child);
1437                 }
1438                 child = ret;
1439                 ret = talloc_asprintf("(\n"
1440                                       "%s\n"
1441                                       ")\n",
1442                                       child);
1443                 talloc_free(child);
1444                 return ret;
1445                 
1446         case LDB_OP_NOT:
1447                 child =
1448                         parsetree_to_sql(
1449                                 module,
1450                                 hTalloc,
1451                                 t->u.not.child);
1452                 ret = talloc_asprintf(hTalloc,
1453                                       "(\n"
1454                                       "  SELECT eid\n"
1455                                       "    FROM ldb_entry\n"
1456                                       "    WHERE eid NOT IN %s\n"
1457                                       ")\n",
1458                                       child);
1459                 talloc_free(child);
1460                 return ret;
1461                 
1462         default:
1463                 /* should never occur */
1464                 abort();
1465         };
1466         
1467         /* Get a case-folded copy of the attribute name */
1468         pAttrName = ldb_casefold((struct ldb_context *) module,
1469                                  t->u.simple.attr);
1470         
1471         /*
1472          * For simple searches, we want to retrieve the list of EIDs that
1473          * match the criteria.  We accomplish this by searching the
1474          * appropriate table, ldb_attr_<attributeName>, for the eid
1475          * corresponding to all matching values.
1476          */
1477         if (t->u.simple.value.length == 1 &&
1478             (*(const char *) t->u.simple.value.data) == '*') {
1479                 /*
1480                  * Special case for "attr_name=*".  In this case, we want the
1481                  * eid corresponding to all values in the specified attribute
1482                  * table.
1483                  */
1484                 if ((p = sqlite3_mprintf("(\n"
1485                                          "  SELECT eid\n"
1486                                          "    FROM ldb_attr_%q\n"
1487                                          ")\n",
1488                                          pAttrName)) == NULL) {
1489                         return NULL;
1490                 }
1491                 
1492                 if (lsqlite3_debug) {
1493                         printf("%s\n", p);
1494                 }
1495
1496                 ret = talloc_strdup(hTalloc, p);
1497                 sqlite3_free(p);
1498                 
1499         } else if (strcasecmp(t->u.simple.attr, "objectclass") == 0) {
1500                 /*
1501                  * For object classes, we want to search for all objectclasses
1502                  * that are subclasses as well.
1503                  */
1504                 if ((p = sqlite3_mprintf(
1505                              "(\n"
1506                              "  SELECT eid\n"
1507                              "    FROM ldb_attr_OBJECTCLASS\n"
1508                              "    WHERE attr_name IN\n"
1509                              "      (SELECT class_name\n"
1510                              "         FROM ldb_objectclasses\n"
1511                              "         WHERE tree_key GLOB\n"
1512                              "           (SELECT tree_key\n"
1513                              "              FROM ldb_objectclasses\n"
1514                              "              WHERE class_name = %Q) || '*')\n"
1515                              ")\n",
1516                              t->u.simple.value.data)) == NULL) {
1517                         return NULL;
1518                 }
1519                 
1520                 if (lsqlite3_debug) {
1521                         printf("%s\n", p);
1522                 }
1523
1524                 ret = talloc_strdup(hTalloc, p);
1525                 sqlite3_free(p);
1526                 
1527         } else {
1528                 /* A normal query. */
1529                 if ((p = sqlite3_mprintf("(\n"
1530                                          "  SELECT eid\n"
1531                                          "    FROM ldb_attr_%q\n"
1532                                          "    WHERE attr_value = %Q\n"
1533                                          ")\n",
1534                                          pAttrName,
1535                                          t->u.simple.value.data)) == NULL) {
1536                         return NULL;
1537                 }
1538                 
1539                 if (lsqlite3_debug) {
1540                         printf("%s\n", p);
1541                 }
1542
1543                 ret = talloc_strdup(hTalloc, p);
1544                 sqlite3_free(p);
1545         }
1546         return ret;
1547 }
1548
1549
1550 static int
1551 parsetree_to_attrlist(struct lsqlite3_private * lsqlite3,
1552                       const struct ldb_parse_tree * t)
1553 {
1554         int                         i;
1555         
1556         switch(t->operation) {
1557         case LDB_OP_SIMPLE:
1558                 break;
1559                 
1560         case LDB_OP_EXTENDED:
1561 #warning  "work out how to handle bitops"
1562                 return -1;
1563
1564         case LDB_OP_AND:
1565                 if (parsetree_to_attrlist(
1566                             lsqlite3,
1567                             t->u.list.elements[0]) != 0) {
1568                         return -1;
1569                 }
1570                 
1571                 for (i = 1; i < t->u.list.num_elements; i++) {
1572                         if (parsetree_to_attrlist(
1573                                     lsqlite3,
1574                                     t->u.list.elements[i]) != 0) {
1575                                 return -1;
1576                         }
1577                 }
1578                 
1579                 return 0;
1580                 
1581         case LDB_OP_OR:
1582                 if (parsetree_to_attrlist(
1583                             lsqlite3,
1584                             t->u.list.elements[0]) != 0) {
1585                         return -1;
1586                 }
1587                 
1588                 for (i = 1; i < t->u.list.num_elements; i++) {
1589                         if (parsetree_to_attrlist(
1590                                     lsqlite3,
1591                                     t->u.list.elements[i]) != 0) {
1592                                 return -1;
1593                         }
1594                 }
1595                 
1596                 return 0;
1597                 
1598         case LDB_OP_NOT:
1599                 if (parsetree_to_attrlist(lsqlite3,
1600                                           t->u.not.child) != 0) {
1601                         return -1;
1602                 }
1603                 
1604                 return 0;
1605                 
1606         default:
1607                 /* should never occur */
1608                 abort();
1609         };
1610         
1611         QUERY_NOROWS(lsqlite3,
1612                      FALSE,
1613                      "INSERT OR IGNORE INTO " FILTER_ATTR_TABLE "\n"
1614                      "    (attr_name)\n"
1615                      "  VALUES\n"
1616                      "    (%Q);",
1617                      t->u.simple.attr);
1618         return 0;
1619 }
1620
1621
1622 /*
1623  * Use the already-generated FILTER_ATTR_TABLE to create a list of attribute
1624  * table names that will be used in search queries.
1625  */
1626 static char *
1627 build_attr_table_list(void * hTalloc,
1628                       struct lsqlite3_private * lsqlite3)
1629 {
1630         int             ret;
1631         int             bLoop;
1632         char *          p;
1633         char *          pAttrName;
1634         char *          pTableList;
1635         sqlite3_stmt *  pStmt;
1636         
1637         /*
1638          * Prepare and execute the SQL statement.  Loop allows retrying on
1639          * certain errors, e.g. SQLITE_SCHEMA occurs if the schema changes,
1640          * requiring retrying the operation.
1641          */
1642         for (bLoop = TRUE; bLoop; ) {
1643                 /* Initialize a string to which we'll append each table name */
1644                 if ((pTableList = talloc_strdup(hTalloc, "")) == NULL) {
1645                         return NULL;
1646                 }
1647                 
1648                 /* Compile the SQL statement into sqlite virtual machine */
1649                 if ((ret = sqlite3_prepare(lsqlite3->sqlite,
1650                                            "SELECT attr_name "
1651                                            "  FROM " FILTER_ATTR_TABLE ";",
1652                                            -1,
1653                                            &pStmt,
1654                                            NULL)) == SQLITE_SCHEMA) {
1655                         continue;
1656                 } else if (ret != SQLITE_OK) {
1657                         ret = -1;
1658                         break;
1659                 }
1660                 
1661                 /* Loop through the returned rows */
1662                 for (ret = SQLITE_ROW; ret == SQLITE_ROW; ) {
1663                         
1664                         /* Get the next row */
1665                         if ((ret = sqlite3_step(pStmt)) == SQLITE_ROW) {
1666                                 
1667                                 /*
1668                                  * Get value from this row and append to table
1669                                  * list
1670                                  */
1671                                 p = discard_const_p(char,
1672                                                     sqlite3_column_text(pStmt,
1673                                                                         0));
1674                                 
1675                                 pAttrName =
1676                                         ldb_casefold(
1677                                                 hTalloc,
1678                                                 sqlite3_column_text(pStmt, 0));
1679
1680                                 /* Append it to the table list */
1681                                 if ((p = talloc_asprintf(
1682                                              hTalloc,
1683                                              "%sldb_attr_%s",
1684                                              *pTableList == '\0' ? "" : ",",
1685                                              pAttrName)) == NULL) {
1686                                         
1687                                         talloc_free(pTableList);
1688                                         return NULL;
1689                                 }
1690                                 
1691                                 /* We have a new table list */
1692                                 talloc_free(pTableList);
1693                                 pTableList = p;
1694                         }
1695                 }
1696                 
1697                 if (ret == SQLITE_SCHEMA) {
1698                         talloc_free(pTableList);
1699                         continue;
1700                 }
1701                 
1702                 /* Free the virtual machine */
1703                 if ((ret = sqlite3_finalize(pStmt)) == SQLITE_SCHEMA) {
1704                         (void) sqlite3_finalize(pStmt);
1705                         continue;
1706                 } else if (ret != SQLITE_OK) {
1707                         (void) sqlite3_finalize(pStmt);
1708                         ret = -1;
1709                         break;
1710                 }
1711                 
1712                 /*
1713                  * Normal condition is only one time through loop.  Loop is
1714                  * rerun in error conditions, via "continue", above.
1715                  */
1716                 ret = 0;
1717                 bLoop = FALSE;
1718         }
1719
1720         if (ret != 0) {
1721                 talloc_free(pTableList);
1722                 pTableList = NULL;
1723         }
1724
1725         return pTableList;
1726 }
1727
1728
1729 /*
1730  * Issue a series of SQL statements to implement the ADD/MODIFY/DELETE
1731  * requests in the ldb_message
1732  */
1733 static int
1734 msg_to_sql(struct ldb_module * module,
1735            const struct ldb_message * msg,
1736            long long eid,
1737            int use_flags)
1738 {
1739         int                         flags;
1740         char *                      pAttrName;
1741         unsigned int                i;
1742         unsigned int                j;
1743         struct lsqlite3_private *   lsqlite3 = module->private_data;
1744         
1745         for (i = 0; i < msg->num_elements; i++) {
1746                 const struct ldb_message_element *el = &msg->elements[i];
1747                 
1748                 if (! use_flags) {
1749                         flags = LDB_FLAG_MOD_ADD;
1750                 } else {
1751                         flags = el->flags & LDB_FLAG_MOD_MASK;
1752                 }
1753                 
1754                 /* Get a case-folded copy of the attribute name */
1755                 pAttrName = ldb_casefold((struct ldb_context *) module,
1756                                          el->name);
1757                 
1758                 if (flags == LDB_FLAG_MOD_ADD) {
1759                         /* Create the attribute table if it doesn't exist */
1760                         if (new_attr(module, pAttrName) != 0) {
1761                                 return -1;
1762                         }
1763                 }
1764                 
1765                 /* For each value of the specified attribute name... */
1766                 for (j = 0; j < el->num_values; j++) {
1767                         
1768                         /* ... bind the attribute value, if necessary */
1769                         switch (flags) {
1770                         case LDB_FLAG_MOD_ADD:
1771                                 QUERY_NOROWS(lsqlite3,
1772                                              FALSE,
1773                                              "INSERT INTO ldb_attr_%q\n"
1774                                              "    (eid, attr_value)\n"
1775                                              "  VALUES\n"
1776                                              "    (%lld, %Q);",
1777                                              pAttrName,
1778                                              eid, el->values[j].data);
1779                                 QUERY_NOROWS(lsqlite3,
1780                                              FALSE,
1781                                              "INSERT INTO ldb_attribute_values"
1782                                              "    (eid, attr_name, attr_value)"
1783                                              "  VALUES "
1784                                              "    (%lld, %Q, %Q);",
1785                                              eid,
1786                                              el->name,
1787                                              el->values[j].data);
1788                                 
1789                                 break;
1790                                 
1791                         case LDB_FLAG_MOD_REPLACE:
1792                                 QUERY_NOROWS(lsqlite3,
1793                                              FALSE,
1794                                              "UPDATE ldb_attr_%q\n"
1795                                              "  SET attr_value = %Q\n"
1796                                              "  WHERE eid = %lld;",
1797                                              pAttrName,
1798                                              el->values[j].data,
1799                                              eid);
1800                                 QUERY_NOROWS(lsqlite3,
1801                                              FALSE,
1802                                              "UPDATE ldb_attribute_values "
1803                                              "  SET attr_value = %Q "
1804                                              "  WHERE eid = %lld "
1805                                              "    AND attr_name = %Q;",
1806                                              el->values[j].data,
1807                                              eid,
1808                                              el->name);
1809                                 break;
1810                                 
1811                         case LDB_FLAG_MOD_DELETE:
1812                                 /* No additional parameters to this query */
1813                                 QUERY_NOROWS(lsqlite3,
1814                                              FALSE,
1815                                              "DELETE FROM ldb_attr_%q\n"
1816                                              "  WHERE eid = %lld\n"
1817                                              "    AND attr_value = %Q;",
1818                                              pAttrName,
1819                                              eid,
1820                                              el->values[j].data);
1821                                 QUERY_NOROWS(lsqlite3,
1822                                              FALSE,
1823                                              "DELETE FROM ldb_attribute_values"
1824                                              "  WHERE eid = %lld "
1825                                              "    AND attr_name = %Q "
1826                                              "    AND attr_value = %Q;",
1827                                              eid,
1828                                              el->name,
1829                                              el->values[j].data);
1830                                 break;
1831                         }
1832                 }
1833         }
1834         
1835         return 0;
1836 }
1837
1838
1839
1840 static int
1841 new_dn(struct ldb_module * module,
1842        char * pDN,
1843        long long * pEID)
1844 {
1845         int                         nComponent;
1846         int                         bFirst;
1847         char *                      p;
1848         char *                      pPartialDN;
1849         long long                   eid;
1850         struct ldb_dn *             pExplodedDN;
1851         struct ldb_dn_component *   pComponent;
1852         struct ldb_context *        ldb = module->ldb;
1853         struct lsqlite3_private *   lsqlite3 = module->private_data;
1854         
1855         /* Explode and normalize the DN */
1856         if ((pExplodedDN =
1857              ldb_explode_dn(ldb,
1858                             pDN,
1859                             ldb,
1860                             case_fold_attr_required)) == NULL) {
1861                 return -1;
1862         }
1863         
1864         /* Allocate a string to hold the partial DN of each component */
1865         if ((pPartialDN = talloc_strdup(ldb, "")) == NULL) {
1866                 return -1;
1867         }
1868         
1869         /* For each component of the DN (starting with the last one)... */
1870         eid = 0;
1871         for (nComponent = pExplodedDN->comp_num - 1, bFirst = TRUE;
1872              nComponent >= 0;
1873              nComponent--, bFirst = FALSE) {
1874                 
1875                 /* Point to the component */
1876                 pComponent = pExplodedDN->components[nComponent];
1877                 
1878                 /* Add this component on to the partial DN to date */
1879                 if ((p = talloc_asprintf(ldb,
1880                                          "%s%s%s",
1881                                          pComponent->component,
1882                                          bFirst ? "" : ",",
1883                                          pPartialDN)) == NULL) {
1884                         return -1;
1885                 }
1886                 
1887                 /* No need for the old partial DN any more */
1888                 talloc_free(pPartialDN);
1889                 
1890                 /* Save the new partial DN */
1891                 pPartialDN = p;
1892                 
1893                 /*
1894                  * Ensure that an entry is in the ldb_entry table for this
1895                  * component.  Any component other than the last one
1896                  * (component 0) may already exist.  It is an error if
1897                  * component 0 (the full DN requested to be be inserted)
1898                  * already exists.
1899                  */
1900                 QUERY_NOROWS(lsqlite3,
1901                              FALSE,
1902                              "INSERT %s INTO ldb_entry\n"
1903                              "    (peid, dn)\n"
1904                              "  VALUES\n"
1905                              "    (%lld, %Q);",
1906                              nComponent == 0 ? "" : "OR IGNORE",
1907                              eid, pPartialDN);
1908                 
1909                 /* Get the EID of the just inserted row (the next parent) */
1910                 eid = sqlite3_last_insert_rowid(lsqlite3->sqlite);
1911
1912                 /* If this is the final component, also add DN attribute */
1913                 if (nComponent == 0) {
1914                         QUERY_NOROWS(lsqlite3,
1915                                      FALSE,
1916                                      "INSERT %s INTO ldb_attr_DN\n"
1917                                      "    (eid, attr_value) "
1918                                      "  VALUES "
1919                                      "    (%lld, %Q);",
1920                                      nComponent == 0 ? "" : "OR IGNORE",
1921                                      eid, pPartialDN);
1922                 }
1923         }
1924         
1925         /* Give 'em what they came for! */
1926         *pEID = eid;
1927         
1928         return 0;
1929 }
1930
1931
1932 static int
1933 new_attr(struct ldb_module * module,
1934          char * pAttrName)
1935 {
1936         long long                   bExists;
1937         struct lsqlite3_private *   lsqlite3 = module->private_data;
1938         
1939         /*
1940          * NOTE:
1941          *   pAttrName is assumed to already be case-folded here!
1942          */
1943         
1944         /* See if the table already exists */
1945         QUERY_INT(lsqlite3,
1946                   bExists,
1947                   FALSE,
1948                   "SELECT COUNT(*) <> 0\n"
1949                   "  FROM sqlite_master\n"
1950                   "  WHERE type = 'table'\n"
1951                   "    AND tbl_name = 'ldb_attr_%q';",
1952                   pAttrName);
1953         
1954         /* Did it exist? */
1955         if (! bExists) {
1956                 /* Nope.  Create the table */
1957                 QUERY_NOROWS(lsqlite3,
1958                              FALSE,
1959                              "CREATE TABLE ldb_attr_%q\n"
1960                              "(\n"
1961                              "  eid        INTEGER REFERENCES ldb_entry,\n"
1962                              "  attr_value TEXT\n"
1963                              ");",
1964                              pAttrName);
1965         }
1966         
1967         return 0;
1968 }
1969
1970