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